Image not setting in ImageView after taking picture from Camera - android

I've have develop my own camera app for mandatory configuration, when i try to show the captured image in next activity which displays whether to save it or not?
I'm not able to fetch the image which i captured and displays on my ImageView. I'm absolutely getting absPathUri with proper path.
Code snippets:-
imgView = (ImageView)findViewById(R.id.picView);
Bundle b= getIntent().getExtras();
absPathUri = Uri.parse(b.getString("URI"));
Toast.makeText(getApplicationContext(), ""+absPathUri, Toast.LENGTH_SHORT).show();
if(absPathUri!=null)
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
imgView.setImageURI(absPathUri);
}
On Further dive in the reason why I'm unable to set the ImageView, throws the Null Pointer Exception which is due to File Not Found. If applicatoin is in debug mode it displays the proper Image on ImageView. It seems that mediaStore get Refreshed till the debugging hits.
File imgFile = new File(getPath(absPathUri));
Toast.makeText(getApplicationContext(), "ImageFile Exists"+imgFile.exists(), Toast.LENGTH_SHORT).show();
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imgView.setImageBitmap(myBitmap);
}
public String getPath(Uri photoUri) {
String filePath = "";
if (photoUri != null) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
try
{
Cursor cursor = getContentResolver().query(photoUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_SHORT).show();
}
}
return filePath;
}
Tried Solution:-
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
Can anybody guide me where I'm getting wrong in displaying Image on ImageView?

As suggested by gnuanu, setImageURI() is not better to use as reading and decoding on the UI thread, which can cause a latency hiccup.
Better to use the following:-
setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.
Still these methods didn't solve my problem. As I was taking Picture with Camera and onClick of it sending to Next Activity which may cause latency hiccups.
So better to pass to another activity after some peroid of time . . just a sec is totally convient to get through it.
Snippet which solve my issue:-
final Intent picIntent = new Intent(CameraActivity.this,PicSaveActivity.class);
picIntent.putExtra("URI", pathUri.toString());
Handler handler = new Handler()
{
public void handleMessage(Message msg) {
startActivityForResult(picIntent, PICTAKEN);
};
};
Message msg = handler.obtainMessage();
handler.sendMessageDelayed(msg, 1000);
In Next Activity, catch the URI and rotate the Image as it would be in landscape mode.
if(absPathUri!=null)
{
Bitmap myImg = BitmapFactory.decodeFile(absPathUri.getPath());
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
matrix, true);
imgView.setImageBitmap(rotated);
}

I don't really know what's wrong in your code, and my answer uses a completely different approach to taking a photo with the camera and displaying it in an ImageView, but it's a working solution that might help you.
In your first Activity (call it CameraActivity), you can do the following:
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class CameraActivity extends PortraitActivity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private Bitmap photo;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
}
/** Method to open camera and take photo */
public void takePhoto(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
/** Method called after taking photo and coming back to current Activity */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", photo);
startActivity(intent);
}
}
}
In the second activity (call it NewActivity), just put the following code in the onCreate method, after having assigned imageView to the corresponding view in the XML layout.
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
imageView.setImageBitmap(photo);

The reference clearly states: The path to the file is contained in the Intent.mData field.
Therefore, you need
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, absPathUri));
You probably also need to let the broadcast complete before you get the results; using
imgView.post(new Runnable() { public void run() { imgView.setImageURI(absPathUri); } }
should suffice.

Related

On button click choose image from gallery and set in another activity's imageView

I have two activities in one i have a button and in second i have a ImageView, Now i want if i click on first activity's button then i go to gallery and pick an image when i select an image then it will automatically set in second activity's ImageView, My code is working when i set image in same acticity's imageView but i want set the image in second activity's ImageView. Please tell me what code i need to write in SecondActivity, My first Activity is(Select_Image):-
public class Select_Image extends Activity {
Button button;
public static final int PICK_FROM_GALLERY = 100;
Bitmap bitmap;
String imagePath = " ";
Uri uri;
ImageView image;
String path = " ";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.select_image);
button = (Button)findViewById(R.id.select_btn);
image = (ImageView)findViewById(R.id.edit_image_id); //This is second Activity's ImageView id.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
galleryIntent();
}
});
}
public void galleryIntent(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_FROM_GALLERY);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == MainActivity.RESULT_OK) {
if (requestCode == PICK_FROM_GALLERY){
}
onSelectFromGalleryResult(data);
}
}
public void onSelectFromGalleryResult(Intent data){
if (data != null) {
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
}
catch (Exception e) {
e.printStackTrace();
}
}
uri = getImageUri(this, bitmap);
imagePath = getRealPathFromUri(uri);
image.setImageBitmap(bitmap);
}
private String getRealPathFromUri(Uri uri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = this.getContentResolver().query(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String ss = cursor.getString(column_index);
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private Uri getImageUri(Select_Image select_image, Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Toast.makeText(select_image, "Image uploaded Successfully", Toast.LENGTH_LONG).show();
path = MediaStore.Images.Media.insertImage(select_image.getContentResolver(), bitmap, "Title", null);
return Uri.parse(path);
}
}
Second Activity(MainActivity)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
In your onActivityResult, you got the result which is used by your current activity. If an image is selected you got your result on onActivityResult.Here, you can pass this imageUrl in your second activity and open image in your second activity. If you have still any doubt you can ask me.
To sum up, you want to interact with Activity A, start a task then receive the result in Activity B, it's impossible. But you can get the same behavior by 2 options:
Start Activity B on Activity A's button clicked. In Activity B's onCreate, start the image selector and get the result from there.
Start the image selector in A's button clicked, get the result and then pass the result to B using startActivity Intent.
Prefer the first option because it makes the code cleaner, Activity B does its own job and Activity A doesn't need to know about it. Moreover, you save some performance when serialize/deserialize the param from A to B, make the activity transition smoother

ImageView disappear after changed orientation

I have just started in developing in Android. So far I am learning stuff. I am stuck with this problem. In my onCreate method, i have a button to select an image in the device and the onActivityResult to get the image and display the image in an ImageView
After researching and looking for answers in the internet, someone advice that I am missing the onSaveInstanceState. so I created it. Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button)findViewById(R.id.buttonLoadPicture);
targetImage = (ImageView) findViewById(R.id.imgView);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMG);
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putParcelable("BitmapImage", bitmap);
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
image = savedInstanceState.getParcelable("BitmapImage");
targetImage.setImageBitmap(image);
}
#Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK){
Uri targetUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(targetUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
this.bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
After building and running the code, selecting image and displaying it in ImageView works fine. When i change the orientation, image selected is still displayed. However when i rotate back again, the image displayed is gone.
Where am i doing wrong. Any advice and help is appreciated.
Are you storing 'bitmap' correctly? Here you are storing it to a local variable:
Bitmap bitmap;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
And here you are using a field:
savedInstanceState.putParcelable("BitmapImage", bitmap);
Looks like the field isn't being set so the onSaveInstanceState is storing a null value. Try removing Bitmap bitmap; or change bitmap = BitmapFactory... to this.bitmap = BitmapFactory.
EDIT:
Also try storing bitmap here:
image = savedInstanceState.getParcelable("BitmapImage");
targetImage.setImageBitmap(image);
By changing it to:
image = savedInstanceState.getParcelable("BitmapImage");
this.bitmap = image;
targetImage.setImageBitmap(this.bitmap);
I'm not sure what image is though as you haven't include the delcaration in the post.

Save bitmap when change layout

I've a layout to portrait and a layout to landscape. In those layout, there is a ImageView with onClick listener. My ImageView has a default image (a camera icon)
When I push into ImageView, open a dialog and I can open camera and take a picture or open gallery and select a image.
Then, I save a bitmap and put it in the ImageView. This bitmap is a global variable in my class.
But when I turn my app, load my other layout (main_landscape) and if I took a pic, I lose my bitmap and charge default image.
This is my code:
public void takePicture(View view) {
final CharSequence[] options = { "Take a pic", "Gallery","Cancel" };
//Build an AlertDialog object
AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
builder.setTitle("Picture");
//onClickListener method
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int selected) {
if(options[selected].equals("Take a pic")){
//open camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
//this is my global bitmap and I put it null when I push take a picture
resized=null;
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
else if(options[selected].equals("Gallery")){
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
else{
dialog.dismiss();
}
//if user doesn't select anything, show default icon
//show picture is my layout ImageView and cameraicon is my default icon
showPicture.setImageResource(R.drawable.cameraicon);
}
});
builder.show();
}
This is my onActivityResult code:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
//if my resultCode is RESULT_OK
if(resultCode == RESULT_OK){
if(requestCode==1){
File f = new File(Environment.getExternalStorageDirectory().toString());
for(File temp: f.listFiles()){
if(temp.getName().equals("temp.jpg")){
f=temp;
break;
}
}
try{
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
newHeight=600;
newWidth=900;
//resized is my global bitmap
resized = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
//show my new bitmap in my ImageView
showPicture.setImageBitmap(resized);
} catch (Exception e) {
e.printStackTrace();
}
}
//I choose gallery option
else if(requestCode==2){
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage,filePath,null,null,null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
newHeight=600;
newWidth=900;
//my global bitmap
resized = Bitmap.createScaledBitmap(thumbnail, newHeight, newWidth, true);
//show new bitmap in my ImageView
showPicture.setImageBitmap(resized);
}
}
}
When I turn my phone, I loose my bitmap. What I can do, please?
Thanks a lot!!
I think you save the value of your Bitmap by calling onSaveInstaceState(Bundle bundle) and then restore it when the activity is recreated.
You might want to store the filename to your photo file inside the activity:
class MyActivity {
File imageFile = null;
}
Since your activity will get destroyed on rotation, you need to save that image file name in onSaveInstanceState():
public void onSaveInstanceState(Bundle outState)
{
if (imageFile != null) {
outState.putString("IMAGE_FILENAME", imageFile.getAbsolutePath());
}
}
Bitmaps in android are Parcelable, so instead of storing the filename of the image you could also save the Bitmap itself.
The saved state gets passed to onCreate() when your activity gets re-created:
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
if (savedState != null) {
String imagePath = saveState.getString("IMAGE_FILENAME");
if (imagePath != null) {
imageFile = new File(imagePath);
}
}
updateImageView();
}
public void updateImageView()
{
// code from onActivityResult() to load the image
}
Side Notes:
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp: f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
You are writing to the global, external storage directory. Please don't do this, use your application's files' directory. Also, no need to search for a fixed filename in the path, simply check if the file exists.
File imageFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), imageFileName);
if (imageFile.exists()) {
[...]
}
I think better way to do it is using fragment instead activity.
Use method fragment.setRetainInstance(true), and fragment will not be destroyed, when you turn your device.

custom gallery cropping window in android

After struggling with Camera intents for too long, I have now finally created a custom camera experience for my app by using SurfaceView and the Camera api. It’s pretty awesome as I now have complete control over the picture taking experience. I would like to take control over the gallery experience as well. It’s not clear how I might do that? Any advice? Basically I am hoping for the following user experience
click on gallery button to view photos in your gallery
select a photo from the gallery
the selected photo shows up in a customized preview in which the user can move the photo around to select which portion to use. Notice that this is a sort of cropping that happens within a fixed area (in my case a square the size of the phone's width): no resizing allowed; the area of the picture within the window is the area used. Pretty much most of the social media apps out there do it that way. I just don’t have a clue how they manage that preview/edit-window.
What I know so far:
I can still use the gallery intent to let the user select the image
The part where I am stuck is on how to create the custom edit experience that lets the user choose an area of the image to show (fixed size square). Again, this is not resize-cropping; but rather the user can move the image around to choose which sub-area fits the window.
Thanks for any advice
Ok Here We Go!
Activity To Select The Image From Gallery.
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
public class GalleryUtil extends Activity{
private final static int RESULT_SELECT_IMAGE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String TAG = "GalleryUtil";
String mCurrentPhotoPath;
File photoFile = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
//Pick Image From Gallery
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_SELECT_IMAGE);
}catch(Exception e){
e.printStackTrace();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case RESULT_SELECT_IMAGE:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
try{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
//return Image Path to the Main Activity
Intent returnFromGalleryIntent = new Intent();
returnFromGalleryIntent.putExtra("picturePath",picturePath);
setResult(RESULT_OK,returnFromGalleryIntent);
finish();
}catch(Exception e){
e.printStackTrace();
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
}else{
Log.i(TAG,"RESULT_CANCELED");
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
break;
}
}
}
Activity To Crop The Selected Image:
public class ImageSelecter extends Activity{
private final int GALLERY_ACTIVITY_CODE=200;
private final int RESULT_CROP = 400;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn_choose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Start Activity To Select Image From Gallery
Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
break;
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_ACTIVITY_CODE) {
if(resultCode == Activity.RESULT_OK){
picturePath = data.getStringExtra("picturePath");
//perform Crop on the Image Selected from Gallery
performCrop(picturePath);
}
}
if (requestCode == RESULT_CROP ) {
if(resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
// Set The Bitmap Data To ImageView
image_capture1.setImageBitmap(selectedBitmap);
image_capture1.setScaleType(ScaleType.FIT_XY);
}
}
}
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 280);
cropIntent.putExtra("outputY", 280);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
This all hope this will give you a head up:)

Getting the file path from a picture in ImageView

Alright I am making an Android app, and using the CAPTURE_PIC_REQUEST to take a picture and store it as a preview on the screen in an imageviewer. I wanted to know how to get the Uri and actually path because I am planning on taking that information, saving it and accessing it later. So I can't figure how to get the Uri or path name from that picture I just took. Here is the code from my activity that is handling
package com.CS480;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AddEx extends Activity {
static final int CAMERA_PIC_REQUEST = 1337;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
Button camera = (Button) findViewById(R.id.picButton);
camera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.returnedPic);
image.setImageBitmap(thumbnail);
}
}
}
So how from that image that I am getting back on the app do I get the file location
As far as I know, you can do something like this:
private Uri mImageCaptureUri;
public class AddEx extends Activity {
static final int CAMERA_PIC_REQUEST = 1337;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
Button camera = (Button) findViewById(R.id.picButton);
camera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent =
new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.returnedPic);
image.setImageBitmap(thumbnail);
String pathToImage = mImageCaptureUri.getPath();
// pathToImage is a path you need.
// If image file is not in there,
// you can save it yourself manually with this code:
File file = new File(mImageCaptureUri.getPath());
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // You can choose any format you want
}
}
From Android documentation about android.provider.MediaStore.EXTRA_OUTPUT:
The name of the Intent-extra used to indicate a content resolver Uri
to be used to store the requested image or video.
You can try getting the URI of the image with
data.getExtras.get("URI");
but this might not work. What you can do is simply save the Bitmap yourself. See this question for a guide on how to do that: Save bitmap to location
When you take your image, you must store it somewhere to get the location of the image.
To do this, you can store to the MediaStore:
String result = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "", "");
imageFileUri = Uri.parse(result);
Try this get image file path form Bitmap
public void uploadBitmap(Context mContext, Bitmap bitmap) {
String imagePath = null;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Uri uri;
Cursor cursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, MediaStore.Images.Media.DATE_ADDED, null, "date_added DESC");
if (cursor != null && cursor.moveToFirst()) {
do {
uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
imagePath = uri.toString();
Log.d("pathatka", uri.toString());
break;
} while (cursor.moveToNext());
cursor.close();
}
}
The android documentation has an example of defining a file URI for saving the output when you setup the Intent for your image capture.

Categories

Resources