来源:https://blog.csdn.net/anjingshuai/article/details/84682779
开发过程中碰到将文件存储到手机中时,要先判断是否有sd卡,如下所示
-
-
private static boolean ExistSDCard() {
-
if (android.os.Environment.getExternalStorageState().equals(
-
android.os.Environment.MEDIA_MOUNTED)) {
-
-
-
-
如果存在,则要获取sd卡的根目录路径,在目录下创建新的文件夹,sd卡根目录路径如下:
-
public static String SDCARDPATH = Environment.getExternalStorageDirectory()
-
然后是将要复制的文件写到sd卡下新建的文件夹内,代码如下:
-
private void copyzipfileToLocalDir(final String path, final String filename) {
-
File file = new File(path);
-
-
Uri uri = Uri.fromFile(file);
-
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
-
intent.setClass(MainActivity.this, TestActivity.class);
-
-
-
-
pdlog = new ProgressDialog(this);
-
pdlog.setMessage("正在复制文件...");
-
-
-
-
-
-
InputStream input = getApplicationContext().getAssets()
-
-
-
-
-
-
File file = f.getParentFile();
-
-
-
-
-
FileOutputStream fout = new FileOutputStream(f);
-
byte[] buff = new byte[1024];
-
-
while ((len = input.read(buff)) > 0) {
-
fout.write(buff, 0, len);
-
-
-
-
-
-
-
-
handler.sendEmptyMessage(1);
-
-
-
-
private Handler handler = new Handler() {
-
-
public void handleMessage(android.os.Message msg) {
-
-
-
-
-
-
-
-
-
-
File file = new File(SDCARDPATH+ "androidtest.pdf");
-
-
-
Uri uri = Uri.fromFile(file);
-
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
-
intent.setClass(MainActivity.this, TestActivity.class);
-
-
-
-
-
-
-
-
-
这样就将assets下的文件写入了外置sd卡,对于一些不支持外置存储卡的Android手机,我们可以将文件写入机身内存,也就是俗称的ROM中,RomPath= Environment.getDataDirectory().getPath();当判断到没有外置sd卡时就可以把path换成这个RomPath即可,这样就完成了将文件写入机身内存中。
Android开发之sd卡存储和机身存储的路径获取
原文:https://www.cnblogs.com/LiTZen/p/12188771.html