android - java.io.FileNotFoundException:没有这样的文件或目录
问题描述
我已经读取了片段中光标处的所有图像文件,并传输了第一个有任何问题的活动,但是当跳转另一个活动时,虽然文件已经存在,但没有读取相同的文件路径。
片段设置所有媒体数据并传输第一个活动。第一个活动正在查看所有媒体文件的现有状态并且所有文件都返回 true,但是如果第一个活动传输这些文件,则第二个活动不会读取相同的文件,并且当调用文件操作 (BitmapFactory.decodeFile) 时会抛出 FileNotFoundException。
isFile()
函数只提供检查是否有文件。
媒体型号:
public Media(Uri uri, String path){
this.uri = uri;
this.path = path;
}
MyFragment.java:
private final LinkedList<Media> mediasList;
void setMediasListAndStartFirstActivity(){
while (cursor != null && cursor.moveToNext()) {
Uri uri = Uri.withAppendedPath(contentUri, cursor.getString(cursor.getColumnIndexOrThrow(Images.Media._ID)));
String path = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DATA));
mediasList.add(new Media(uri, path));
}
Intent i = new Intent(requireActivity(), FirstActivity.class);
i.putExtra("mediasList", mediasList);
startActivity(i);
}
FirstActivity.java:
Bundle extras = getIntent().getExtras();
mediasList = (ArrayList<Media>) getIntent().getExtras().get("mediasList");
for(Media media : mediasList)
boolean hasMedia = new File(media.getPath()).isFile(); // --> all medias return true
void transferAllMediasToSecondActivity(){
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("mediasList", mediasList);
startActivity(i);
}
SecondActivity.java
Bundle extras = getIntent().getExtras();
mediasList = (ArrayList<Media>) getIntent().getExtras().get("mediasList");
for(int i = mediasList.size; i <= 0; i--)
{
Media media = mediasList.get(i);
File mediaFile = new File(media.getPath());
boolean hasMedia = mediaFile.isFile(); // --> all medias return false. All medias are not reading
// -> throw FileNotFound exception call this function
BitmapFactory.decodeFile(mediaFile.getAbsolutePath(), new BitmapFactory.Options());
}
我现有的媒体第一路径:
/storage/emulated/0/DCIM/Screenshots/Screenshot_20190712-123350.jpg
在 MyFragment
new File(media.getPath()).isFile()
:真在 FirstActivity
new File(media.getPath()).isFile()
:真在 SecondActivity
new File(media.getPath()).isFile()
: false 和 decodeFile 函数抛出 FileNotFoundException
例外:
无法解码流:java.io.FileNotFoundException:/storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg(没有这样的文件或目录)
FirstFragment mediasList 日志:
媒体[0]:com.android.android.conversation.attachment.gallery.model.Media@2ee333c9 媒体[1]:com.android.android.conversation.attachment.gallery.model.Media@2ee333b1 媒体[2]:com .android.android.conversation.attachment.gallery.model.Media@2ee333b0
媒体[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg 媒体[1].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182334910.jpg 媒体[2] .getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190716_170439762.jpg
FirstActivity 和 SecondActivity mediasList 日志:
媒体[0]:com.android.android.conversation.attachment.gallery.model.Media@2ee333c9 媒体[1]:com.android.android.conversation.attachment.gallery.model.Media@2ee333b1
媒体[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg 媒体[1].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182334910.jpg
解决方案
尝试更改从片段中获取路径的方式:
while (cursor != null && cursor.moveToNext()) {
Uri uri = Uri.withAppendedPath(contentUri, cursor.getString(cursor.getColumnIndexOrThrow(Images.Media._ID)));
String path = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DATA));
mediasList.add(new Media(uri, path));
}
到
while (cursor != null && cursor.moveToNext()) {
Uri uri = Uri.withAppendedPath(contentUri, cursor.getString(cursor.getColumnIndexOrThrow(Images.Media._ID)));
String path = uri.getPath();
mediasList.add(new Media(uri, path));
}
推荐阅读
- javascript - 在 ReactJS 项目中使用 Google 图表
- cmake - 如何将源代码编译成 ARM 二进制文件
- perl - 如何在 SBATCH 脚本中调用 Perl 脚本以进行 SLURM 提交?
- haskell - 使用 megaparsec 链接一元运算符
- c# - 获取一个字符列表并替换不在列表中的字符串中的任何字母
- javascript - 尝试从链接提交表单同时调用函数
- c++ - 如何从 std::vector 转换
到 std::istream? - mobile - 移动设备上的 Apache 超集
- ffmpeg - 使用 ffmpeg 流式传输到 youtube
- python-3.x - 如何在 Python 中计算积分估计?