g.r.
g.r.

Reputation: 43

File path from Camera Intent

I'm trying to get the file path of pictures that I took with Camera Intent as a String, but the String filePath is always null. What am I doing wrong?

       public void onClick(View arg0) {
        switch (arg0.getId()) {     
         case R.id.btnImageCapture:
            Intent openCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
               startActivityForResult(openCamera, OPEN_CAMERA); 
                 break;
           }
        }



        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         switch(requestCode){
         case OPEN_CAMERA:
             if (resultCode == RESULT_OK && data != null) {
                 Uri captureImage = data.getData();
                 String filePath = captureImage.getPath();
                 break;
              } 
           }
        }

Upvotes: 1

Views: 346

Answers (2)

Droidman
Droidman

Reputation: 11608

try the following passing your captureImage Uri as parameter:

    public String getRealPathFromURI(Uri contentUri) {
        String[] projx = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, projx, null, null, null);
         int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

EDIT:

that is a common bug that getData() returns null on some devices. You need to use a pre-inserted Uri to prevent that. Example:

  Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
 preinsertedUri = getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new ContentValues());

        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

Getting the result:

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

    switch (requestCode) {

    case CAMERA_PIC_REQUEST:

        if (resultCode != 0 && data != null) {

            Uri imageUri = preinsertedUri;
            }
              break; 
                 }

Upvotes: 1

StuStirling
StuStirling

Reputation: 16221

This is how I get the image that was taken with the camera.

I create the file before and when the image gets saved then it gets saved to my file..

File externalFile = new File("Whatever you want the path to be...");
Uri uriSavedImage=Uri.fromFile(externalFile);
Intent launchcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
launchcameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(launchcameraIntent,CAMERA_PIC_REQUEST);

Then when the result is received.

protected void onActivityResult(int requestCode,int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAMERA_PIC_REQUEST) {
                  Bitmap photo = BitmapUtils.decodeFileForDisplay(new File("Whatever your file's path is");
            }
        }
}

Upvotes: 1

Related Questions