Hello Droid Friends,
Today , I am sharing the code for capture Image from Camera and Gallery . Actually
I found different behavior of Camera capture Intent and Gallery Intent on different
Android devices .
Here are the few issue I found while capturing Image from Camera and Gallery:
1. In some devices like Samsung the Camera capture Intent returns
data null or some time gallery Intent returns data null.
2. Android Camera : data intent returns null
3. Android camera capture activity returns null Uri
4. onActivityResult Camera resulting data as null (SAMSUNG)
I too faces all these issue, then I uses below code which works dine for me.
Code:.
1. MainActivity.java:
2. ImageUtils.java
Download code : Camera and Gallery Demo
Hope this will help some one.
Enjoy Coding.... :)
Today , I am sharing the code for capture Image from Camera and Gallery . Actually
I found different behavior of Camera capture Intent and Gallery Intent on different
Android devices .
Here are the few issue I found while capturing Image from Camera and Gallery:
1. In some devices like Samsung the Camera capture Intent returns
data null or some time gallery Intent returns data null.
2. Android Camera : data intent returns null
3. Android camera capture activity returns null Uri
4. onActivityResult Camera resulting data as null (SAMSUNG)
I too faces all these issue, then I uses below code which works dine for me.
Code:.
1. MainActivity.java:
package com.gallerycamera.demo;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static int THUMBNAIL_SIZE = 300;
private static final int YOUR_SELECT_PICTURE_REQUEST_CODE = 232;
private Button button;
private ImageView image;
private Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.activity_main_image);
button = (Button) findViewById(R.id.activity_main_button);
button.setOnClickListener(buttonListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (bmp != null && !bmp.isRecycled()) {
bmp.recycle();
bmp = null;
}
}
private View.OnClickListener buttonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Determine Uri of camera image to save.
FileUtils.createDefaultFolder(MainActivity.this);
//final File file = FileUtils.createFile(FileUtils.IMAGE_FILE);
//outputFileUri = Uri.fromFile(file);
// Camera.
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
final Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//galleryIntent.setType("image/*");
// Filesystems
// galleryIntent.setAction(Intent.ACTION_GET_CONTENT); // To allow file managers or any other app that are not gallery app.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Image");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent });
startActivityForResult(chooserIntent, YOUR_SELECT_PICTURE_REQUEST_CODE);
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
//if (resultCode == Activity.RESULT_OK) {
if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Uri selectedImage = data.getData();
if (selectedImage != null) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
bmp = ImageUtils.getThumbnail(this,filePath, THUMBNAIL_SIZE);
image.setImageBitmap(bmp);
bmp = null;
}
}
}
//}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
2. ImageUtils.java
package com.gallerycamera.demo;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageUtils {
/**
* Get a thumbnail bitmap.
* @param uri
* @return a thumbnail bitmap
* @throws FileNotFoundException
* @throws IOException
*/
public static Bitmap getThumbnail(Context context, String filePath, int thumbnailSize) throws FileNotFoundException, IOException {
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeFile(filePath, onlyBoundsOptions);
if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
return null;
int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
double ratio = (originalSize > thumbnailSize) ? (originalSize / thumbnailSize) : 1.0;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither = true;// optional
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
Bitmap bitmap = BitmapFactory.decodeFile(filePath, bitmapOptions);
return bitmap;
}
/**
* Resolve the best value for inSampleSize attribute.
* @param ratio
* @return
*/
private static int getPowerOfTwoForSampleRatio(double ratio) {
int k = Integer.highestOneBit((int) Math.floor(ratio));
if (k == 0)
return 1;
else
return k;
}
}
Download code : Camera and Gallery Demo
Hope this will help some one.
Enjoy Coding.... :)














