in my application you can click on the image, which gives you the opportunity to select the image from the SD card or make a new image using the camera. I have already made a choice to select an image from an SD card. Now I have a camera option.
My question is: how can I get the path to the image of any new image taken with the camera?
Below you will find the code that I use to open a dialog box and the way I get the path to images from the SD card.
1.This is the code for the dialogue:
private void showImageDialog() { final String [] items = new String [] {"From Camera", "From SD Card"}; ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select Image"); builder.setAdapter( adapter, new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int item ) { if (item == 0) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory(), "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); mImageCaptureUri = Uri.fromFile(file); try { intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri); intent.putExtra("return-data", true); startActivityForResult(intent, PICK_FROM_CAMERA); } catch (Exception e) { e.printStackTrace(); } dialog.cancel(); } else { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE); } } } ); final AlertDialog dialog = builder.create(); dialog.show(); }
2. And this is what is used to get the image path from the image of the SD card.
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == PICK_FROM_FILE || requestCode == PICK_FROM_CAMERA) { Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); Log.v("IMAGE PATH====>>>> ",selectedImagePath);
source share