我正在构建一个可以从照片库获取图像的android应用。
如果可以的话,两个问题:
1)
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String action = intent.getAction();
Uri uri = null;
// if this is from the share menu
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM))
{
try
{
// Get resource path from intent callee
uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
我有这个uri:
content://media/external/images/media/27031
但是我的代码在这里失败,没有任何信息(它只是崩溃了),没有日志记录,没有控制台错误。
try {
if (uri !=null)
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
Log.e(this.getClass().getName(), e.toString());
}
我该如何解决?
(它适用于以下网址:
Uri uri = Uri.parse("content://media/external/images/media/22719");
2)我计划将所有uri保存在数据结构中。那是推荐的方法吗?
还是将照片保存在本地应用程序文件夹中更好?
更新资料
我认为问题在于失败的映像位于外部SD上,而后续的映像位于本地SD上。
如何启用来自外部SD的图像?
最佳答案
一些用于从照片的“共享”动作获取位图的代码...
Object obj = null;
if (Intent.ACTION_SEND.equals(_action) && _type != null) {
obj = _bundle.get("android.intent.extra.STREAM");
if( getIntent().getType().startsWith("image/") && obj != null){
if (obj instanceof android.net.Uri){ get((Uri)obj);}}
get(Uri uri){
Bitmap _bit = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri),null,options);
}