【发布时间】:2026-01-17 08:45:01
【问题描述】:
我已读取片段中光标处的所有图像文件,并转移了第一个有任何问题的活动,但是在跳转另一个活动时,虽然文件已经存在,但没有读取相同的文件路径。
Fragment 设置所有媒体数据并传输第一个活动。第一个活动正在查看所有媒体文件的现有状态并且所有文件都返回 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
在我的片段
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 媒体列表日志:
媒体[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
【问题讨论】:
-
尝试在 secondActivity 中打印出
mediasList。 -
我打印出来请帮帮我
标签: android file filenotfoundexception