onActivityResult使用Intent.EXTRA_ALLOW_MULTIPLE时,得到RESULT_CANCELLED [英] onActivityResult getting RESULT_CANCELLED when using Intent.EXTRA_ALLOW_MULTIPLE

查看:2786
本文介绍了onActivityResult使用Intent.EXTRA_ALLOW_MULTIPLE时,得到RESULT_CANCELLED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的活动,在打开的画廊选择单个或多个图像下面的按钮,下面这一点, OnActivityResult 函数,即返回结果为 RESULT_CANCELLED 的多个图像,而 RESULT_OK 为一个单一的形象。不知道为什么它的发生。是否有人可以帮忙。

I have the following button in my activity, that opens the gallery to select single or multiple images, and below this, the OnActivityResult function, that is returning result as RESULT_CANCELLED for multiple images, and RESULT_OK for a single image. Not sure why it's happening. Can someone please help.

buttonGallery.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setType("image/*");
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), choose_picture);
    //startActivity(intent);
    }
});

//OnActivityResult for the above

public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(requestCode == choose_picture) {
          Uri imageUri = (Uri)data.getParcelableExtra(Intent.EXTRA_STREAM);
      //Do something
}

我收到 data.getData() data.getExtras ()

有人能指导我如何获得所需结果从上面的code。我想的URI 用户从库中进行选择。所有图像

Can someone guide me how to get the required results from the above code. I want the URIs of all images that the user selects from the gallery.

PS:它的正常工作为一个单一的形象,不知道为什么。

PS : It's working fine for a single image, not sure why.

推荐答案

我终于得到了解决这个。当使用 EXTRA_ALLOW_MULTIPLE ,当有多个内容的用户选择,而不是在返回intent.getExtra(),从意向中的数据 ClipData ,它仅支持SDK版本18及更高返回。从那里,将数据可以使用以下code进行检索 - >

Finally I got the solution to this. When using EXTRA_ALLOW_MULTIPLE, when there is more than one content that the user is selecting, instead of being returned in intent.getExtra(), the data from the intent is returned in ClipData, which is only supported for SDK versions 18 and higher. From there, the data can be retrieved using the following code ->

 if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) 
                                            && (null == data.getData())) 
 {
                ClipData clipdata = data.getClipData();
                for (int i=0; i<clipdata.getItemCount();i++)
                {
                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), clipdata.getItemAt(i).getUri());
                        //DO something
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
}

我已经把空检查 intent.getData(),因为如果一个单一的形象,这个数据被接收意图。的getData(),而在情况多重选择的,这是收到的

I've put the null check for intent.getData() because in case of a single image, the data is received in intent.getData(), while in case of multiple selection, this is received as null.

因此​​,对于低于18 SDK版本,以及单一选择(不论SDK版本),数据可以以下面的方式被简单地检索

So, for sdk versions below 18, and for single selection (irrespective of sdk version), the data can be simply retrieved in the following manner :

InputStream ist = this.getContentResolver()
                            .openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(ist);

这篇关于onActivityResult使用Intent.EXTRA_ALLOW_MULTIPLE时,得到RESULT_CANCELLED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆