0

I made an app. It has 2 options for uploading photo:

1) by taking a photo using camera

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);

2) by picking from gallery

Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);

My problem is getting file path after these intents in onActivityResult. Is there any methods to get those paths for new File(path) , that also takes care of sdk level changes? For example till KitKat is 1 type of file system , after KitKat is other type.

test
  • 13
  • 7

2 Answers2

1

My problem is getting file path after these intents in onActivityResult.

There will be no file path for the ACTION_IMAGE_CAPTURE approach, because you did not provide EXTRA_OUTPUT. If you do provide EXTRA_OUTPUT, then you already know what the file path is.

There is no file path for ACTION_PICK, insofar as there is no requirement that what the user picks be in a file that you have access to. For example, it could be an image on removable storage. Use a ContentResolver and methods like openInputStream() to get the content represented by the Uri that you are given.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • onActivityResult returns me the Uri of the file i have chosen from Gallery, sorry if i have explained bad above, but i was meaning to get the correct file path based on that Uri – test Mar 04 '16 at 17:52
  • @test: Again, there is no way to get a file path that you can necessarily access. Use a `ContentResolver` and methods like `openInputStream()` to get the content represented by the `Uri` that you are given. "onActivityResult returns me the Uri of the file i have chosen from Gallery" -- only if you consider the `ACTION_PICK` scenario to be "chosen from Gallery". `ACTION_IMAGE_CAPTURE` is not documented to return a `Uri`, and most camera apps will not return a `Uri` as a result. – CommonsWare Mar 04 '16 at 17:57
0

If you can able to get path from camera then for Intent.ACTION_PICK you can directly get uri of image using data.getData() and then you can get file path using this method(as suggested in the link provided by Gennadii Saprykin

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); }
Brijesh Chopda
  • 195
  • 2
  • 11