Android - 如何从ClipData获取图像?

Bar*_*urg 8 upload android image android-photos

我正在创建一个图像上传器,它能够一次上传多个图像.

galerijButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);   
    }
});
Run Code Online (Sandbox Code Playgroud)

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            if(imageReturnedIntent.getData() != null){
                //If uploaded with Android Gallery (max 1 image)
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                    photos.add(yourSelectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                //If uploaded with the new Android Photos gallery
                ClipData clipData = imageReturnedIntent.getClipData();
                for(int i = 0; i < clipData.getItemCount(); i++){
                    clipData.getItemAt(i);
                    //What now?
                }
            }
        }
    break;
    ....
Run Code Online (Sandbox Code Playgroud)

我想将所有选中的图像添加到我的照片阵列中ArrayList<Bitmap>.不知何故,我必须转换ClipData.ItemBitmap,但如何?

SMR*_*SMR 14

试试这个

ClipData.Item item = clip.getItemAt(i);
Uri uri = item.getUri();
Run Code Online (Sandbox Code Playgroud)

现在你有了图像的URI.

我想你现在知道该怎么做了.干杯:)