简体   繁体   中英

Get File object onActivityResult when select Image

I am able to get a Bitmap from images in android, but I'm in trouble when I try to get a File object. My question is how can I get a File object when onActivityResult returns.

Here goes my code:

To open images selection:

private void setUpAddMorePicturesClick() {
    Button btnAddPictures = (Button)findViewById(R.id.btn_add_pictures);
    btnAddPictures.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);//
            startActivityForResult(Intent.createChooser(intent, getString(R.string.text_select_picture)),
                                        ConstantValues.REQUEST_CODE_SELECT_IMAGE);
        }
    });
}

OnActivityResult Method:

public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ConstantValues.REQUEST_CODE_SELECT_IMAGE)
        {
            if (resultCode == Activity.RESULT_OK)
            {
                if (data != null)
                {

                //Uri uri = data.getData();
                //String uriString = uri.toString();
                //File imgFile = new File(uriString);

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();


                File imgFile = new File(picturePath); // <-- NOT WORKING

               // File imgFile = Environment.getExternalStoragePublicDirectory(data.getData().getPath());



                //Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
                //byte[] s = Utils.bitmapToByteArray(bitmap);

                Snackbar snackbarError = Snackbar.make(rootRelativeLayout, R.string.msg_error_wcf_call_default, Snackbar.LENGTH_LONG);
                //new EscortBusiness(this).addImage(s, snackbarError);
                new EscortBusiness(this).addImageMultiPart(imgFile, snackbarError);

            }
        } else if (resultCode == Activity.RESULT_CANCELED)
        {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
        }
    }
}

使用URI中的inputstream导致其架构可能是file://或content://

BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM