android - 将完整文件夹复制到android 10中的另一个文件夹
问题描述
我在 android 10 中有一个应用程序,我将图像保存在下面的路径中。
File f = new File(context.getExternalFilesDir(null), "myImages");
此路径将图像保存在 myImages 文件夹中,但众所周知,对于 android 10,此文件夹将在 Android/data/app.packagename/files/myImages 中创建,因此当我卸载应用程序时,图像文件夹也会被删除。我为解决这个问题所做的是,现在我使用下面的代码将图像保存在图片文件夹中。
final String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + "myImages";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
final ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, Calendar.getInstance().getTime().toString());
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
final ContentResolver resolver = context.getContentResolver();
OutputStream stream = null;
Uri uri = null;
try
{
final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
uri = resolver.insert(contentUri, contentValues);
if (uri == null)
{
throw new IOException("Failed to create new MediaStore record.");
}
stream = resolver.openOutputStream(uri);
if (stream == null)
{
throw new IOException("Failed to get output stream.");
}
if (inImage.compress(Bitmap.CompressFormat.JPEG, 100, stream) == false)
{
throw new IOException("Failed to save bitmap.");
}
}
catch (IOException e)
{
if (uri != null)
{
// Don't leave an orphan entry in the MediaStore
resolver.delete(uri, null, null);
}
e.printStackTrace();
}
finally
{
if (stream != null)
{
stream.close();
}
}
return uri;
这很好用,所有图像现在都保存到图片文件夹,但问题是在我的第一个应用程序更新中存在不同的路径,而在第二次更新中它具有新图片的路径。因此,我试图在此应用程序更新中将文件从旧路径移动到新路径,以便如果旧路径中有任何图像,则可以将它们复制到新路径并且应用程序将运行良好。
我使用此代码复制数据。
String fromdirectory = getExternalFilesDir(null).getAbsolutePath()+"/tempBooks/";
File from = new File(getExternalFilesDir(null).getAbsolutePath()+"/tempBooks/");
final String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + "tempBooks";
try{
FileUtils.copyFile(fromdirectory, relativeLocation);
} catch (Exception e) {
e.printStackTrace();
}
但它给出了一个例外
java.io.FileNotFoundException: /storage/emulated/0/Android/data/app.package.name/files/myImages: open failed: EISDIR (Is a directory)
**请帮我解决这个问题,我也尝试了其他几种方法,但仍然存在此异常
解决方案
推荐阅读
- java - Arduino 可以从控制台读取字节,但不能从 java
- ios - 连接硬件键盘时保持 UIInputViewController 可见
- python - 无法找到两个日期时间变量之间的差异
- java - 如何检索保存为介质 blob 的多个图像,然后将图像保存到 arrayList 中?
- python - Python“三元组”字典?
- c - 以固定大小的位数组为键查找表
- git - VS Code:暂存编辑会取消同一文件中的其他编辑
- javascript - 如何成功调用此函数以返回值?JSON.parse()
- python-3.x - 有没有办法比较用户名和密码是否都是dict login_info中的有效对?
- java - 在 spring-releases (http://repo.spring.io/libs-release) 中找不到工件 org.springframework:springloaded:jar:2.1.3.RELEASE