1

我正在构建一个可以从照片库中获取图像的 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 获取图像?

4

1 回答 1

0

一些用于从照片上的“共享”操作中获取位图的代码......

    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);
}
于 2013-09-14T12:36:38.383 回答