【问题标题】:java.io.FileNotFoundException: No such file or directoryjava.io.FileNotFoundException:没有这样的文件或目录
【发布时间】: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


【解决方案1】:

尝试改变从片段中获取路径的方式:

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));
}

【讨论】:

  • 所有媒体均未通过此方法显示第一个片段、活动和第二个活动:D
  • @propoLis 你能用来自片段和第一个活动的 mediasList 日志更新你的问题吗?
  • 我写的 :)
【解决方案2】:

我注意到我的 parcelable Media 并非到处都是 parcelable。

我找到了这个解决方案:

Android E/Parcel﹕ Class not found when unmarshalling (only on Samsung Tab3)

  Bundle b = new Bundle();
  b.putParcelableArrayList(KEY_MEDIA, medias);
  intent.putExtra("bundle", b);

  Bundle b = getIntent().getBundleExtra("bundle");
  sendMediasList = b.getParcelableArrayList(KEY_MEDIA);

我用过它,它工作正常。非常感谢@John Joe 和 @Frandall 的努力

【讨论】:

    最近更新 更多