【问题标题】:MediaStore.Images.Media.getBitmap fails unexpectedlyMediaStore.Images.Media.getBitmap 意外失败
【发布时间】:2013-09-18 13:35:04
【问题描述】:

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

【问题讨论】:

    标签: java android android-intent bitmap android-studio


    【解决方案1】:

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

        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);
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2021-12-04
    • 2018-01-02
    • 2017-09-20
    • 2013-05-25
    • 2017-07-23
    • 2016-06-22
    相关资源
    最近更新 更多