Reputation: 230
This is a demo application that i use to capture picture with the phone camera app, do some compression and display it on imageview. It works on other devices except on nexus devices. Tested on LG e975, Samsung S3 mini, Samsung Note 2 and it works fine. I try to search for it but no luck finding any. Here is the code:
main activity:
public class Main extends Activity {
protected ImageView img_pic;
protected Button btn_capture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_pic = (ImageView) findViewById(R.id.imageView);
btn_capture = (Button) findViewById(R.id.button);
btn_capture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
}
});
}
public static String getBitmapFromCameraData(Intent data, Context context) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return picturePath;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if (data != null && requestCode == 1) {
Bitmap bitmap_camera_1 = BitmapFactory.decodeFile(getBitmapFromCameraData(data, this));
int height_1 = bitmap_camera_1.getHeight(), width_1 = bitmap_camera_1.getWidth();
if (height_1 > 1280 && width_1 > 960) {
Bitmap image_1 = BitmapFactory.decodeFile(getBitmapFromCameraData(data, this), options);
image_1.compress(Bitmap.CompressFormat.JPEG, 100, stream);
img_pic.setImageBitmap(image_1);
System.out.println("Need to resize camera");
} else {
bitmap_camera_1.compress(Bitmap.CompressFormat.JPEG, 100, stream);
img_pic.setImageBitmap(bitmap_camera_1);
System.out.println("WORKS camera");
}
} else {
Toast.makeText(this, "Something went wrong, please try again", Toast.LENGTH_LONG).show();
}
}
}
log:
10-08 10:34:32.662 2998-2998/com.gilbert.apptastic.picturetest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.gilbert.apptastic.picturetest, PID: 2998
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.gilbert.apptastic.picturetest/com.gilbert.apptastic.picturetest.Main}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1420)
at android.content.ContentResolver.query(ContentResolver.java:445)
at android.content.ContentResolver.query(ContentResolver.java:404)
at com.gilbert.apptastic.picturetest.Main.getBitmapFromCameraData(Main.java:44)
at com.gilbert.apptastic.picturetest.Main.onActivityResult(Main.java:61)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Thanks.
Upvotes: 0
Views: 537
Reputation: 57163
See https://code.google.com/p/android/issues/detail?id=57996 - it's a reported bug four some Nexus devices. The workaround suggested there,
intent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
should be used with care, because it is not compatible with some other devices.
See also http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/.
Upvotes: 1