I have an android application which contains multiple activities.
In one of them I\'m using a button which will call the device camera :
public void
When we will capture the image from Camera in android then Uri or data.getdata()
comes null. we have two solutions to resolve this issue.
I will implement all methods here, Please carefully watch and read these:-
First i will tell how to get Uri from Bitmap Image: Complete code is :
First we will capture image through Intent that will same for both methods so this code i will write one time only here :
// Capture Image
captureImg.setOnClickListener(new View.OnClickListener(){
@Override public void onClick(View view){
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager())!=null){
startActivityForResult(intent,reqcode);
}
}
});
Now we will Implement OnActivityResult :-(This will be same for both above 2 methods):-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == reqcode && resultCode == RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView.setImageBitmap(photo);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
//Show Uri path based on Image
Toast.makeText(LiveImage.this, "Here " + tempUri, Toast.LENGTH_LONG).show();
//Show Uri path based on Cursor Content Resolver
Toast.makeText(this, "Real path for URI : " + getRealPathFromURI(tempUri), Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Failed To Capture Image", Toast.LENGTH_SHORT).show();
}
}
Now we will create all above method to create Uri from Image and Cursor methods via classes:
Now URI path from Bitmap Image
private Uri getImageUri(Context applicationContext, Bitmap photo)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(LiveImage.this.getContentResolver(), photo, "Title", null);
return Uri.parse(path);
}
\ Uri from Real path of saved image
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);
}