1

您可能已经注意到,在较新的 Android 版本上,Android 现在有 2 个图片库:图库和照片。将来,谷歌将弃用并删除图库,只保留照片库。

我想让用户能够使用 Android API 级别 18+“.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);”选择多个图像进行上传。问题是,如果用户选择从图库上传,则该功能不起作用(只能选择一张图片)。

如何确保应用程序自动尝试通过 Android Photo 的应用程序上传?

到目前为止,这是我的代码:

galerijButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        if(Build.VERSION.SDK_INT >= 18 ){
            photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        }
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);   
    }
});

..

@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);
                    // more code logic
                }
            }
        }
    break;
4

1 回答 1

0

您可以将意图与具有呼叫照片应用程序包名称的操作一起使用。示例 com.google.exactnameofphotoapp。然后它会导致您想要的应用程序。

并使用

 if(Build.VERSION.SDK_INT >= 18 ){
...
}

用于新的 api 功能。

于 2014-02-13T10:18:29.283 回答