gpt4 book ai didi

java - 从 SD 卡获取图像时出现 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-01 13:17:56 25 4
gpt4 key购买 nike

我正在尝试获取 SD 卡中的所有图像并填充到 gridview,,,,

这是我的代码,用于从 SD 卡获取图像

private ArrayList<CustomGallery> getGalleryPhotos() {
ArrayList<CustomGallery> galleryList = new ArrayList<CustomGallery>();

try {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;

@SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
if (imagecursor != null && imagecursor.getCount() > 0) {

while (imagecursor.moveToNext()) {
CustomGallery item = new CustomGallery();

int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);

item.sdcardPath = imagecursor.getString(dataColumnIndex);
galleryList.add(item);
}
}
}catch (Exception e) {
e.printStackTrace();
}

// show newest photo at beginning of the list
Collections.reverse(galleryList);
return galleryList;
}

上面的代码对某些图像返回 FileNotFoundException,但在 gridview 中填充可用图像,gridview 为 enter image description here

前五个网格没有图像,选择这些图像时会在其他 Activity 中给出空白区域

抛出的异常是

03-11 11:00:40.540: E/ImageLoader(4145): /mnt/storage/Kushalam/_profileImage/CroppedImage.jpg (No such file or directory)
03-11 11:00:40.540: E/ImageLoader(4145): java.io.FileNotFoundException: /mnt/storage/Kushalam/_profileImage/CroppedImage.jpg (No such file or directory)
03-11 11:00:40.540: E/ImageLoader(4145): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
03-11 11:00:40.540: E/ImageLoader(4145): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
03-11 11:00:40.540: E/ImageLoader(4145): at java.io.FileInputStream.<init>(FileInputStream.java:80)
03-11 11:00:40.540: E/ImageLoader(4145): at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:82)
03-11 11:00:40.540: E/ImageLoader(4145): at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:180)
03-11 11:00:40.540: E/ImageLoader(4145): at java.net.URL.openStream(URL.java:645)
03-11 11:00:40.540: E/ImageLoader(4145): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromFile(BaseImageDownloader.java:121)
03-11 11:00:40.540: E/ImageLoader(4145): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:82)
03-11 11:00:40.540: E/ImageLoader(4145): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:340)
03-11 11:00:40.540: E/ImageLoader(4145): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:232)

最佳答案

我认为 mediastore 返回错误的路径。
尝试检查路径中的真实文件。

CustomGallery item = new CustomGallery();

int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);

item.sdcardPath = imagecursor.getString(dataColumnIndex);
File temp = new File( item.sdcardPath );
if( temp.exists() )
galleryList.add(item);

像这样。

关于java - 从 SD 卡获取图像时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com