简体   繁体   中英

Set ImageView with picture taken by the user

I would like to set the drawable source of my ImageView with the picture taken by the user but it's not working.

This is my onActivityResult when the user take a picture :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
          Intent intent = new Intent(this, MyNewActivity.class);
          intent.putExtra("URI", imageUri.toString());
          startActivity(intent);
    }
}

And this is the part of the Activity where I want to set the resource of my ImageView :

if (getIntent().getStringExtra("URI") != null) {
    File imgFile = new File(getIntent().getStringExtra("URI"));

    if(imgFile.exists()){
         Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
         contactImage.setImageBitmap(myBitmap);
    }
}

When I try this, I have a path like this : " file///storage/emulated/O/fname_xxxx.jpg " but it's not working.

I try with a picture picker and it work (with a path like "/storage/emulated/..." )! I really don't know why...

 File imgFile = new File(getIntent().getStringExtra("URI"));

Translates to

 File imgFile = new File("file:///storage/emulated/O/fname_xxxx.jpg");

But it should translate to

 File imgFile = new File("/storage/emulated/O/fname_xxxx.jpg");

So remove "file://" before use.

This worked for me

    if (getIntent().getStringExtra("URI") != null) {
        Uri myUri = Uri.parse(extras.getString("URI"));
        Picasso.with(RegisterActivity.this)
               .load(selectedImage)
               .into(contactImage);
    }

Try Below Code for both Pic taken by Camera & also pick by gallery

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == this.RESULT_OK) {
            if (requestCode == SELECT_FILE)
                onSelectFromGalleryResult(data);
            else if (requestCode == REQUEST_CAMERA)
                onCaptureImageResult(data);
        }
    }

    private void onCaptureImageResult(Intent data) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");
        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        profileIV.setImageBitmap(thumbnail);

        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        Uri tempUri = getImageUri(getApplicationContext(), thumbnail);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
         file = new File(getRealPathFromURI(tempUri));
    }

    @SuppressWarnings("deprecation")
    private void onSelectFromGalleryResult(Intent data) {

        Bitmap bm=null;
        if (data != null) {
            try {
                bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());

                // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
                Uri tempUri = getImageUri(getApplicationContext(), bm);

                // CALL THIS METHOD TO GET THE ACTUAL PATH
                 file = new File(getRealPathFromURI(tempUri));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        profileIV.setImageBitmap(bm);
    }



    public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }

To click Image

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("URI",Intent.URI_INTENT_SCHEME);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

Replace onActivityResult() method with below code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("URI");
        imageView.setImageBitmap(photo);
    }
}

I think you are parcel wrong URI data in intent Bundle try below code

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
          Intent intent = new Intent(this, MyNewActivity.class);
          intent.putExtra("URI", data.getData().toString());
          startActivity(intent);
    }
}

this may help

I found the solution replacing the "file://" by "" :

File imgFile = new File(getIntent().getStringExtra("URI").replace("file://", ""));

So I get :

if (getIntent().getStringExtra("URI") != null) {
            File imgFile = new File(getIntent().getStringExtra("URI").replace("file://", ""));
            if(imgFile.exists()){
                Picasso.with(this).load(imgFile).into(myImageView);
            }
        }

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