Camera Intent Not Working with Samsung Galaxy S3

Camera intent not working with Samsung Galaxy S3

Finally after struggling for three days, I have devised a simple mechanism to overcome the Samsung S3 Saga.

Below is the code, a quick summary of the code is that I first check the Manufacturer of the device and based on this I call different intents for camera.

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;
private static final int PICK_FROM_GALLERY = 2;
int CAMERA_PIC_REQUEST = 1337;
Bitmap thumbnail = null;
private static final int OG = 4;

private static final int CAMERA_IMAGE_CAPTURE = 0;
Uri u;
ImageView imgview;
// int z=0;
String z = null;
byte b[];
String largeImagePath = "";
Uri uriLargeImage;
Uri uriThumbnailImage;
Cursor myCursor;

public void imageCam(Bitmap thumbnail) {
Bitmap photo = thumbnail;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 70, bos);
b = bos.toByteArray();
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(photo);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(onBTN);
if (savedInstanceState != null) {

Bitmap Zatang;
String B1 = savedInstanceState.getString("message");
Toast.makeText(this, "SavedYeah" + B1, Toast.LENGTH_LONG).show();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = OG;
Zatang = BitmapFactory.decodeFile((B1), opts);
System.gc();
if (Zatang != null) {
Toast.makeText(this, "Success Zatang" + B1, Toast.LENGTH_LONG)
.show();
imageCam(Zatang);
}
}
}

private View.OnClickListener onBTN = new View.OnClickListener() {
public void onClick(View v) {
openNewGameDialog();
}
};

String[] B = { "Cam", "Gallery" };

private void openNewGameDialog() {
new AlertDialog.Builder(this).setItems(B,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,int i) {
if (i == 0) {
String BX1 = android.os.Build.MANUFACTURER;

if(BX1.equalsIgnoreCase("samsung")) {
Toast.makeText(getApplicationContext(), "Device man"+BX1, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_IMAGE_CAPTURE);

} else {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
} else if (i == 1) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
}).show();
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_IMAGE_CAPTURE && resultCode==Activity.RESULT_OK){

// Describe the columns you'd like to have returned. Selecting from the Thumbnails location gives you both the Thumbnail Image ID, as well as the original image ID
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA};
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's
MediaStore.Images.Thumbnails.MINI_KIND;

String sort = MediaStore.Images.Thumbnails._ID + " DESC";

//At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
myCursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);

long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";

try {
myCursor.moveToFirst();
imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
} finally {
myCursor.close();
}

//Create new Cursor to obtain the file Path for the large image

String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA
};

String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
largeImagePath = "";

try {
myCursor.moveToFirst();

//This will actually give yo uthe file path location of the image.
largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
} finally {
myCursor.close();
}
// These are the two URI's you'll be interested in. They give you a handle to the actual images
uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));

// I've left out the remaining code, as all I do is assign the URI's to my own objects anyways...
// Toast.makeText(this, ""+largeImagePath, Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show();

if (largeImagePath != null) {
Toast.makeText(this, "LARGE YES"+largeImagePath, Toast.LENGTH_LONG).show();

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = OG;
thumbnail = BitmapFactory.decodeFile((largeImagePath), opts);
System.gc();
if (thumbnail != null) {
Toast.makeText(this, "Try Without Saved Instance", Toast.LENGTH_LONG).show();
imageCam(thumbnail);
}
}
if (uriLargeImage != null) {
Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show();
}
if (uriThumbnailImage != null) {
Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show();
}
}
if( requestCode == 1337 && resultCode== RESULT_OK){
Bundle extras = data.getExtras();
if (extras.keySet().contains("data") ){
BitmapFactory.Options options = new BitmapFactory.Options();
thumbnail = (Bitmap) extras.get("data");
if (thumbnail != null) {
Toast.makeText(this, "YES Thumbnail", Toast.LENGTH_LONG).show();
BitmapFactory.Options opt = new BitmapFactory.Options();
thumbnail = (Bitmap) extras.get("data");
imageCam(thumbnail);
}
} else {
Uri imageURI = getIntent().getData();
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
imageview.setImageURI(imageURI);

if(imageURI != null){
Toast.makeText(this, "YES Image Uri", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
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();
BitmapFactory.Options opts = new BitmapFactory.Options();
thumbnail = BitmapFactory.decodeFile((picturePath), opts);
System.gc();
imageCam(thumbnail);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("message",largeImagePath );
}

}
}

Android Camera Intent not working - Launching gallery instead

MainActivity.java

public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyCameraImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="79dp" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:text="StartCamera" />

</RelativeLayout>

Snap Shot of image saved in Gallery

Sample Image

Android Samsung: Camera app won't return intent.getData()

See Specify filename for picture to be taken for code to specify an EXTRA_OUTPUT parameter for the Intent which lets you specify a filename for the picture. Remember the filename when the activity result is called and use that if the intent.getData is NULL

And if you read the other comments in that bug report, you'll realize how many problems that picture taking on Android has.

Camera Force Closing issue in Samsung Galaxy S3 version 4.1.1

I have Spent many Hours for this Single Issue and Got the Code Working in Most of the Devices With Modifying the Code as Below :

Any one having the Same issue in future can try out the Below Code.

While Calling intent for Image Capture :

String storageState = Environment.getExternalStorageState();

if (storageState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);

String filename = System.currentTimeMillis() + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, filename);
mImageCaptureUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);

intent.putExtra(
android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);

try {

startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else {
new AlertDialog.Builder(BuildInukshk_4_Camera.this)
.setMessage(
"External Storeage (SD Card) is required.\n\nCurrent state: "
+ storageState)
.setCancelable(true).create().show();
}

} else { // pick from file
Intent intent = new Intent();

intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
}

Inside OnActivityResult Method :

case PICK_FROM_CAMERA:
Log.i("TAG", "Inside PICK_FROM_CAMERA");

// Final Code As Below
try {
Log.i("TAG", "inside Samsung Phones");
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA };
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select
// only
// mini's
MediaStore.Images.Thumbnails.MINI_KIND;

String sort = MediaStore.Images.Thumbnails._ID + " DESC";

// At the moment, this is a bit of a hack, as I'm returning ALL
// images, and just taking the latest one. There is a better way
// to
// narrow this down I think with a WHERE clause which is
// currently
// the selection variable
Cursor myCursor = this.managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, null, sort);

long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";

try {
myCursor.moveToFirst();
imageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
} finally {
// myCursor.close();
}

// Create new Cursor to obtain the file Path for the large image

String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA };

String largeFileSort = MediaStore.Images.ImageColumns._ID
+ " DESC";
myCursor = this.managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
largeFileProjection, null, null, largeFileSort);
String largeImagePath = "";

try {
myCursor.moveToFirst();

// This will actually give yo uthe file path location of the
// image.
largeImagePath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
mImageCaptureUri_samsung = Uri.fromFile(new File(
largeImagePath));
mImageCaptureUri = null;
} finally {
// myCursor.close();
}

// These are the two URI's you'll be interested in. They give
// you a
// handle to the actual images
Uri uriLargeImage = Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
String.valueOf(imageId));
Uri uriThumbnailImage = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(thumbnailImageId));

// I've left out the remaining code, as all I do is assign the
// URI's
// to my own objects anyways...
} catch (Exception e) {
mImageCaptureUri_samsung = null;
Log.i("TAG",
"inside catch Samsung Phones exception " + e.toString());

}

try {
Log.i("TAG",
"URI Samsung:" + mImageCaptureUri_samsung.getPath());

} catch (Exception e) {
Log.i("TAG", "Excfeption inside Samsung URI :" + e.toString());
}

try {

Log.i("TAG", "URI Normal:" + mImageCaptureUri.getPath());
} catch (Exception e) {
Log.i("TAG", "Excfeption inside Normal URI :" + e.toString());
}

break;

After Running Below Code you Will get Two URIs mImageCaptureUri_samsung and mImageCaptureUri

you will get the mImageCaptureUri as your Path if you are running the App with Simple Devices and you will get your Cpatured Image path in mImageCaptureUri_samsung if you are running with Devices Like Samsung Galaxy S3.

Further you all can go ahead with your Code. it Works For me Very Fine With all the Devices i have tested on.

Also if Someone is having Problem with Above Code than they can reference the Below Great Link Solution of Samsung Galaxy S3

Hope it will Help.



Related Topics



Leave a reply



Submit