Getting the file path from a picture in ImageView

38,279

Solution 1

As far as I know, you can do something like this:

private Uri mImageCaptureUri;

public class AddEx extends Activity {
  static final int CAMERA_PIC_REQUEST = 1337; 
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

    Button camera = (Button) findViewById(R.id.picButton);
    camera.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent cameraIntent = 
                    new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    mImageCaptureUri);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);               
        }               
    });

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_PIC_REQUEST) {  
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image = (ImageView) findViewById(R.id.returnedPic);  
        image.setImageBitmap(thumbnail);  

        String pathToImage = mImageCaptureUri.getPath();

        // pathToImage is a path you need. 

        // If image file is not in there, 
        //  you can save it yourself manually with this code:
        File file = new File(mImageCaptureUri.getPath());
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // You can choose any format you want
    }  
}    

From Android documentation about android.provider.MediaStore.EXTRA_OUTPUT:

The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video.

Solution 2

When you take your image, you must store it somewhere to get the location of the image.

To do this, you can store to the MediaStore:

String result = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "", "");

imageFileUri = Uri.parse(result);

Solution 3

You can try getting the URI of the image with

data.getExtras.get("URI");

but this might not work. What you can do is simply save the Bitmap yourself. See this question for a guide on how to do that: Save bitmap to location

Share:
38,279
Puresilence
Author by

Puresilence

Updated on November 05, 2020

Comments

  • Puresilence
    Puresilence over 3 years

    Alright I am making an Android app, and using the CAPTURE_PIC_REQUEST to take a picture and store it as a preview on the screen in an imageviewer. I wanted to know how to get the Uri and actually path because I am planning on taking that information, saving it and accessing it later. So I can't figure how to get the Uri or path name from that picture I just took. Here is the code from my activity that is handling

    package com.CS480;
    
    import java.io.File;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class AddEx extends Activity {
      static final int CAMERA_PIC_REQUEST = 1337; 
      public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
    
        Button camera = (Button) findViewById(R.id.picButton);
        camera.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);               
            }               
        });
    
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_PIC_REQUEST) {  
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image = (ImageView) findViewById(R.id.returnedPic);  
            image.setImageBitmap(thumbnail);  
        }  
    }
    
    }
    

    So how from that image that I am getting back on the app do I get the file location