Nexus设备上的Android相机捕获活动错误

Posted

技术标签:

【中文标题】Nexus设备上的Android相机捕获活动错误【英文标题】:Android camera capture activity error on nexus devices 【发布时间】:2014-12-03 04:49:36 【问题描述】:

这是一个演示应用程序,我用它用手机相机应用程序捕获图片,进行一些压缩并将其显示在 imageview 上。它适用于除 nexus 设备之外的其他设备。在 LG e975、三星 S3 mini、三星 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 ResultInfowho=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)

谢谢。

【问题讨论】:

【参考方案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/。

【讨论】:

以上是关于Nexus设备上的Android相机捕获活动错误的主要内容,如果未能解决你的问题,请参考以下文章

为啥使用相机意图捕获的图像会在 Android 上的某些设备上旋转?

android中三星设备上的相机捕捉方向

从相机捕获图像并将其显示在android的另一个活动中

Cordova 媒体捕获插件错误(Nexus 6、Android 6)

从android中的画廊和相机中捕获图像

android中相机捕获的隐式意图