Retrieve Picasa Image for Upload from Gallery

前端 未结 2 1839
夕颜
夕颜 2020-12-14 03:36

I am working on an activity and associated tasks that allow users to select an image to use as their profile picture from the Gallery. Once the selection is made the image

2条回答
  •  甜味超标
    2020-12-14 04:28

    I have faced the exact same problem,
    Finally the solution I found, was to launch an ACTION_GET_CONTENT intent instead of an ACTION_PICK, then make sure you provide a MediaStore.EXTRA_OUTPUT extra with an uri to a temporary file. Here is the code to start the intent :

    public class YourActivity extends Activity {
        File mTempFile;
        int REQUEST_CODE_CHOOSE_PICTURE = 1;
    
        (...)
        public showImagePicker() { 
            mTempFile = getFileStreamPath("yourTempFile");
            mTempFile.getParentFile().mkdirs();
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile));
            intent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());                       
            startActivityForResult(intent,REQUEST_CODE_CHOOSE_PICTURE);
        }
    
        (...)
    }
    

    You might need to mTempFile.createFile()

    Then in onActivityResult, you will be able to get the image this way

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        case REQUEST_CODE_CHOOSE_PICTURE:
                    Uri imageUri = data.getData();
                    if (imageUri == null || imageUri.toString().length() == 0) {
                        imageUri = Uri.fromFile(mTempFile);
                        file = mTempFile;
                    }
                                        if (file == null) {
                                           //use your current method here, for compatibility as some other picture chooser might not handle extra_output
                                        }
    }
    

    Hope this helps

    Then you should delete your temporary file on finish (it is in internal storage as is, but you can use external storage, I guess it would be better).

提交回复
热议问题