这是一个演示应用程序,用于通过手机摄像头应用程序捕获图片,进行一些压缩并在imageview上显示。它适用于除nexus设备之外的其他设备。测试了LG e975,三星S3迷你,三星Note 2,它运行良好。 我尝试搜索它,但没有找到任何运气。 这是代码:
主要活动:
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();
}
}
}
日志:
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)
感谢。
答案 0 :(得分:1)
请参阅https://code.google.com/p/android/issues/detail?id=57996 - 报告了一些Nexus设备的错误。解决方法在那里建议,
intent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
应小心使用,因为它与其他一些设备不兼容。
另见http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/。