从图库中获取图像 - 并非所有“存在”?

时间:2010-09-24 14:26:03

标签: android image android-intent gallery filenotfoundexception

我只是想获取用户选择的图像路径,然后将其转换为位图。问题是,只有部分图像在选中时才能工作(通过“工作”我的意思是它们被发现是存在的文件),而其他图像声称文件不存在(即使图像显示出来)在画廊?)。更奇怪的是,这似乎并不一致,一度被认为“存在”的图像现在声称不存在。我的代码如下:

-----意图-----

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_ACTIVITY);

- - - - - - - - onActivityForResult

Uri uri = intent.getData();
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri,proj,null,null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();

BitmapFactory.Options opts = new BitmapFactory.Options();<br/>
opts.inSampleSize = 2;<br/>
Bitmap b = BitmapFactory.decodeFile(cursor.getString(column_index),opts);

对此有任何想法将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

图库中的某些图片是从外部源(例如Picasa)加载的,因此未存储在本地,导致本地文件路径读取失败。您可以通过阅读uri值来区分它们。我无法找到解决方案,也许这个错误http://code.google.com/p/android/issues/detail?id=21234很快就会引出解决方案。