java.io.FileNotFoundException: No entry for content://media/external/audio/album

xj179 2012-06-01 04:17:03
被 一个小问题搞了好久,弄得人好郁闷至今还没弄明白原因。代码如下 :
private Bitmap getArtworkFromFile(Context context2, long songID, long albumID) {
Bitmap bitmap = null;
if (songID < 0 && albumID < 0) {
throw new IllegalArgumentException("Must specify an album or a song id");
}
BitmapFactory.Options options = new BitmapFactory.Options();
FileDescriptor fd = null;
if (albumID < 0) {
Uri uri = Uri.parse("content://media/external/audio/media/" + songID + "/albumart");
try {
ParcelFileDescriptor dfd = context2.getContentResolver().openFileDescriptor(uri, "r");
if (dfd != null) {
fd = dfd.getFileDescriptor();
}
} catch (FileNotFoundException e) {
// e.printStackTrace();
}

} else {
Uri uri = ContentUris.withAppendedId(artworkUri, albumID);
try {
ParcelFileDescriptor dfd = context2.getContentResolver().openFileDescriptor(uri, "r");

if (dfd != null) {
fd = dfd.getFileDescriptor();
}
} catch (FileNotFoundException e) {
// e.printStackTrace();
}
}
options.inSampleSize = 1;
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, options);

options.inSampleSize = 500;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);
return bitmap;
}

运行的时候它会报:java.io.FileNotFoundException: No entry for content://media/external/audio/album异常,异常的代码是在else中的ParcelFileDescriptor dfd = context2.getContentResolver().openFileDescriptor(uri, "r");。奇怪的事来了:我只把代码的结构改成如下 :
private Bitmap getArtworkFromFile(Context context2, long songID, long albumID) {
Bitmap bitmap = null;
if (songID < 0 && albumID < 0) {
throw new IllegalArgumentException(
"Must specify an album or a song id");
}
try {
BitmapFactory.Options options = new BitmapFactory.Options();
FileDescriptor fd = null;
if (albumID < 0) {
Uri uri = Uri.parse("content://media/external/audio/media/" + songID + "/albumart");
ParcelFileDescriptor dfd = context2.getContentResolver().openFileDescriptor(uri, "r");
if (dfd != null) {
fd = dfd.getFileDescriptor();
}

} else {
Uri uri = ContentUris.withAppendedId(artworkUri, albumID);
ParcelFileDescriptor dfd = context2.getContentResolver().openFileDescriptor(uri, "r");
if (dfd != null) {
fd = dfd.getFileDescriptor();
}

}
options.inSampleSize = 1;
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, options);

options.inSampleSize = 500;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);

} catch (Exception e) {
e.printStackTrace() ;
}
return bitmap;
}

它竟然没报错,看了好久也没看出个眉目来,不知道 是什么原因,不知道各位懂不懂,到底是什么原因?
...全文
965 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧