Android Q Beta中的文件操作 [英] File operations in android Q beta

查看:271
本文介绍了Android Q Beta中的文件操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)使用android.permission.WRITE_EXTERNAL_STORAGE

2)使用getExternalStorageDirectorygetExternalStoragePublicDirectoryFileOutputStream(file)保存文件引发

2) use getExternalStorageDirectoryor getExternalStoragePublicDirectoryand FileOutputStream(file)saving file throws

java.io.FileNotFoundException: /storage/emulated/0/myfolder/mytext.txt open failed: ENOENT (No such file or directory)

3)使用getExternalFilesDir api,保存成功,但即使在MediaScannerConnection.scanFile之后也不会显示.

3) use getExternalFilesDirapi and saving is success but wont show up even after MediaScannerConnection.scanFile.

     /storage/emulated/0/Android/data/my.com.ui/files/Download/myfolder/mytext.txt

在Android Q中将文件从内部存储器复制到SDCARD并刷新的最佳方法是什么.

What is best way to copy file from internal memory to SDCARD in android Q and refresh.

推荐答案

在Android API 29及更高版本中,您可以使用以下代码将文件,图像和视频存储到外部存储中.

In Android API 29 and above you can use the below code to store files, images and videos to external storage.

///首先,如果您使用"android.media.action.IMAGE_CAPTURE"来选择文件 然后将该文件存储在应用程序私有Path(getExternalFilesDir())中,如下所示.

//First, if your picking your file using "android.media.action.IMAGE_CAPTURE" then store that file in applications private Path(getExternalFilesDir()) as below.

File destination = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName);

//然后使用内容提供者访问媒体商店,如下所示.

//Then use content provider to get access to Media-store as below.

 ContentValues values = new ContentValues(); 
 values.put(MediaStore.Images.Media.TITLE, fileName); 
 values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);

///如果需要特定的MIME类型,请在此处指定您的MIME类型.否则将其保留为空白,将使用默认的文件mime类型

//if you want specific mime type, specify your mime type here. otherwise leave it blank, it will take default file mime type

values.put(MediaStore.Images.Media.MIME_TYPE, "MimeType"); 
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000); 
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/" + "path"); // specify 
 storage path

//使用内容提供程序插入媒体存储,它将返回URI.

// insert to media-store using content provider, it will return URI.

Uri uri = cxt.getContentResolver().insert(MediaStore.Files.getContentUri("external"), values); 

//使用该URI打开文件.

//Use that URI to open the file.

ParcelFileDescriptor descriptor = context.getContentResolver().openFileDescriptor(uri,"w"); //"w" specify's write mode
FileDescriptor fileDescriptor = descriptor.getFileDescriptor();

//从私有路径读取文件.

// read file from private path.

InputStream dataInputStream = cxt.openFileInput(privatePath_file_path);

//将文件写入文件流.

//write file into out file-stream.

 OutputStream output = new FileOutputStream(fileDescriptor);
 byte[] buf = new byte[1024];
 int bytesRead;
 while ((bytesRead = dataInputStream.read(buf)) > 0) 
 {
  output.write(buf, 0, bytesRead);
 }
 datanputStream.close();
 output.close();
}

这篇关于Android Q Beta中的文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆