1

is it there a standard way to retrieve the path of an saves image taken by the camera, because i found different ways to display the path but they cause the App to crash. and since i'm new to android programming i just need some guidance. Please check the code:

Java Code:

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My Images");
            imagesFolder.mkdirs();
            File image = new File(imagesFolder, "img01");
            uriSavedImage = Uri.fromFile(image);
            File f = new File (uriSavedImage.getPath());// <---------
            //imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, f.getPath()); // <---------
            startActivityForResult(imageIntent,CAMERA_REQUEST_CODE);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == CAMERA_REQUEST_CODE) && (resultCode == RESULT_OK)) {

            Toast.makeText(getApplicationContext(),// <---------
                    " "+data.getData(), 
                    Toast.LENGTH_SHORT).show();
Androelpha
  • 387
  • 3
  • 8
  • 19

1 Answers1

0

It crashes because you pass getParent() to convertImageUriToFile method (you can see NullPointerException in the stacktrace). According to android docs getParent() returns null if activity is not an embedded one. Yours most probably isn't.

Use File f = convertImageUriToFile(uriSavedImage, this); instead.

For another error, imageUri being null, see this aswer for very possible explanation and solution.

Morover, your imageUri is a "file://" type uri, not "content://" type.To get path from it, use

new File(uri.getPath());

instead of your convertImageUriToFIle() method. I think that method will work only for "content://" type uris.

Actually, you do not need to get the file path from uri you made yourself, you have that path in the line:

File image = new File(imagesFolder, "img01");
Community
  • 1
  • 1
Tomasz Niedabylski
  • 5,768
  • 1
  • 18
  • 20