Reputation: 8425
I am opening camera using following code
Intent captureImageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cordova.setActivityResultCallback(this);
cordova.getActivity().startActivityForResult(captureImageIntent,RESULT_CAPTURE_IMAGE);
and inside onActivityResult
i am trying to get the path of the image
that is stored in gallery , so that i can return it back to the web page.
here what i have tried so far
Uri uri = intent.getData(); // doesnt work
i tried to use MediaStore.EXTRA_OUTPUT
but in that case i am getting null intent.
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
so can anyone tell me how can i fetch the path ?
EDIT
String fileName = "temp.jpg";
contentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
Uri mPhotoUri = cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Upvotes: 0
Views: 1207
Reputation: 24848
Define custom methods for set and get captured image path :
private String imgPath;
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
Uri imgUri = Uri.fromFile(file);
imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
Set image uri to EXTRA_OUTPUT using capture intent :
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
Get captured image bitamp from decoded capture image path :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RESULT_CAPTURE_IMAGE) {
imgUserImage.setImageBitmap(decodeFile(getImagePath()));
}
}
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
Upvotes: 1