0

I am capturing image from the device camera and saving it into my external memory via Uri and I am passing that Uri to next activity in order to display there. I am able to pass the Uri successfully to next activity, but I am unable to set that to imageview. I am bring my head to solve this, refer almost all tutiorals but I am not able to solve this. kindly help me. This is my camera activity code

package kumar.anish.sunset_logistics;


public class underConstruction extends Activity {
ImageView image;
Uri ak;
File photofile;
//   ArrayList arrayList1;
File imageStorageFolder;
static final int CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE = 1888;
public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;
String TAG="Log";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_under_construction);
    image=(ImageView)findViewById(R.id.Imc);

    File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    photofile = new File(photostorage, (System.currentTimeMillis()) + ".jpg");


    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //intent to start camera
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photofile));

    startActivityForResult(i, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

    //Check that request code matches ours:
    if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE)
    {
        saving();

        Intent confirmBMP = new Intent(this,ImagePreview.class);

        Log.e("my dir pass", ak.toString());
        confirmBMP.setData(ak);
        startActivity(confirmBMP);
    }

}
public void saving()
{
    String root = Environment.getExternalStorageDirectory().toString();
    File newDir = new File(root + "/Anish Roger");
    newDir.mkdirs();
    Random gen = new Random();
    int n = 10000;
    n = gen.nextInt(n);
    // String fotoname = "Photo-"+ n +".jpg";
    String fotoname = "Photo.jpg";
    File file = new File (newDir, fotoname);
    if (file.exists ()) file.delete ();
    ak=Uri.fromFile(file);
    Log.e("my dir", ak.toString());

}

}

And this is my next activity where I am trying to set the captured image in the imageview

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_preview);
    imageView=(ImageView)findViewById(R.id.IMcam);
        Intent intent = getIntent();
    Uri uri = getIntent().getData();
    Log.e("Uri in ImagePr",uri.toString());


    imageView.setImageURI(uri);




}

Kindly help me.

Anish Kumar
  • 478
  • 4
  • 12
  • 27

1 Answers1

0

In your 'saving' method, you don't seem to be writing anything to the file (in fact, you delete it if it exists).

The next activity is presumably getting a URI which refers to a file that doesn't exist, so calling imageView.setImageURI(uri) will not work.

You probably want to copy the file captured by the camera app into the file which your "ak" URI points to within your 'saving' method.

Have a look at the FileInputStream and FileOutputStream classes, or use the code from this answer to copy the file.

Community
  • 1
  • 1
sheltond
  • 1,877
  • 11
  • 15