java.io.FileNotFoundException: 没有那个文件或目录
java.io.FileNotFoundException: No such file or directory
我已经读取片段中光标处的所有图像文件并首先传输 activity 有任何问题但是当跳转另一个 activity 相同的文件路径时没有读取虽然文件已经存在。
片段设置所有媒体数据并首先传输activity。第一个 activity 正在查看所有媒体文件的现有状态和所有文件 return true 但如果第一个 activity 传输这些文件,第二个 activity 不读取相同的文件并且在调用时文件操作(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
at MyFragment new File(media.getPath()).isFile()
: true
at FirstActivity new File(media.getPath()).isFile()
: true
at SecondActivity new File(media.getPath()).isFile()
: false and decodeFile function throw FileNotFoundException
异常:
Unable to decode stream: java.io.FileNotFoundException:
/storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg(No
such file or directory)
FirstFragment mediasList 日志:
media[0] :
com.android.android.conversation.attachment.gallery.model.Media@2ee333c9
media[1] :
com.android.android.conversation.attachment.gallery.model.Media@2ee333b1
media[2]:
com.android.android.conversation.attachment.gallery.model.Media@2ee333b0
media[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg
media[1].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182334910.jpg
media[2].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190716_170439762.jpg
FirstActivity 和 SecondActivity mediasList 日志:
media[0] :
com.android.android.conversation.attachment.gallery.model.Media@2ee333c9
media[1] :
com.android.android.conversation.attachment.gallery.model.Media@2ee333b1
media[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg
media[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));
}
我注意到我的可打包媒体没有到处打包。
我找到了这个解决方案:
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
我已经读取片段中光标处的所有图像文件并首先传输 activity 有任何问题但是当跳转另一个 activity 相同的文件路径时没有读取虽然文件已经存在。
片段设置所有媒体数据并首先传输activity。第一个 activity 正在查看所有媒体文件的现有状态和所有文件 return true 但如果第一个 activity 传输这些文件,第二个 activity 不读取相同的文件并且在调用时文件操作(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
at MyFragment
new File(media.getPath()).isFile()
: trueat FirstActivity
new File(media.getPath()).isFile()
: trueat SecondActivity
new File(media.getPath()).isFile()
: false and decodeFile function throw FileNotFoundException
异常:
Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg(No such file or directory)
FirstFragment mediasList 日志:
media[0] : com.android.android.conversation.attachment.gallery.model.Media@2ee333c9 media[1] : com.android.android.conversation.attachment.gallery.model.Media@2ee333b1 media[2]: com.android.android.conversation.attachment.gallery.model.Media@2ee333b0
media[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg media[1].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182334910.jpg media[2].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190716_170439762.jpg
FirstActivity 和 SecondActivity mediasList 日志:
media[0] : com.android.android.conversation.attachment.gallery.model.Media@2ee333c9 media[1] : com.android.android.conversation.attachment.gallery.model.Media@2ee333b1
media[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg media[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));
}
我注意到我的可打包媒体没有到处打包。
我找到了这个解决方案:
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