【问题标题】:Camera intent throws Null Pointer Exception in Sony Phones相机意图在索尼手机中引发空指针异常
【发布时间】:2014-02-18 18:07:11
【问题描述】:

我打算使用Camera Intent,它可以在除Sony C2305 [4.2.2] 之外的其他设备上完美运行。在 4.2.2 模拟器上测试,运行良好。

接下来是 sn-p 。我习惯打电话给Camera Intent

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 getImagePath();
 intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
 startActivityForResult(intent, REQUEST_CAMERA);

 public void getImagePath()
        {
            File imageDirectory =null;
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) 
            {
                imageDirectory = new File(Environment.getExternalStorageDirectory().getPath()+"/ABC");
            }
            else
            {
                imageDirectory = new File(SmartConsultant.getApplication().getApplicationContext().getFilesDir().getAbsolutePath());
            }
            imageDirectory.mkdirs();
            File tempFile = new File(imageDirectory, getVideoName()+ AppConstants.EXTENSION); //AppConstants.Extension is .jpg and getVideoName to fetch name of file as per current sys time.
            outputFileUri = Uri.fromFile( tempFile );
            currentFileUri = outputFileUri;
        }

获取Activity的结果为:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
   if (requestCode == REQUEST_CAMERA) {
            if(resultCode == RESULT_OK)
            {
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                btmapOptions.inSampleSize = 2;
                bm = BitmapFactory.decodeFile(currentFileUri.getPath(), btmapOptions);
                NewExpensesActivity.this.data.add(bm);
                imagesAdapter.notifyDataSetChanged();
                    compressedPath = ImageCompression.compressImage(currentFileUri.getPath());//ADDED 10018
                    galleryAddPic();
                    paths.add(compressedPath);//EDITED 10018

            }
        }
          }

但它在传递结果时抛出空指针异常 原木猫:

 java.lang.RuntimeException: Unable to resume activity {com.smarthumanoid.com/com.netdoers.com.ui.AddSxActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/SmartConsultant/20140217194718.jpg typ=image/jpeg (has extras) }} to activity {com.smarthumanoid.com/com.netdoers.com.ui.AddSxActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2899)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2928)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3865)
at android.app.ActivityThread.access$700(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/SmartConsultant/20140217194718.jpg typ=image/jpeg (has extras) }} to activity {com.smarthumanoid.com/com.netdoers.com.ui.AddSxActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3488)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2883)
... 13 more
 Caused by: java.lang.NullPointerException
at com.netdoers.com.ui.AddSxActivity.onActivityResult(AddSxActivity.java:485)
at android.app.Activity.dispatchActivityResult(Activity.java:5371)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3484)
... 14 more

【问题讨论】:

  • AddSxActivity.java:485在哪里?
  • 查看onActivityResult里面的AddSxActivity.java类的代码。你在那里得到了一些空值。另请发布您的整个活动代码。
  • @SiddharthVyas :但同样的应用程序在其他手机上也能正常工作。只是索尼导致了这些 NPE。请检查已编辑的 SO。
  • @Shayanpourvatan 第 485 行:bm = BitmapFactory.decodeFile(currentFileUri.getPath(), btmapOptions); 在其他手机中运行非常流畅并没有引发 NPE。把它放在 try and catch 块中让我们现在看看。

标签: android sony android-camera-intent


【解决方案1】:

相机意图

           try {
                Log.d("Main Activity", "Camera");

                String fileName = "temp.jpg";  
                ContentValues values = new ContentValues();  
                values.put(MediaStore.Images.Media.TITLE, fileName);  
                mImageCaptureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
                intent.putExtra(MediaStore.EXTRA_OUTPUT,mImageCaptureUri);  

                startActivityForResult(intent, 2);
            } catch (Exception e) {
                // TODO Auto-generated catch block

            }

onActivityResult

       protected void onActivityResult(final int requestCode,final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("Main Activity", "On Activity Result");


    try {
        Uri selectedImage=null;

        if(requestCode == 2 && resultCode == RESULT_OK )
        {

            /* Log.d("Main Activity", "Camera");
              File fi = new File("/sdcard/tmp");
                 try {
                   selectedImage = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), fi.getAbsolutePath(), null, null));
                     if (!fi.delete()) {
                         Log.i("logMarker", "Failed to delete " + fi);
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }*/
            selectedImage=mImageCaptureUri;
            /*          mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                       "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
            selectedImage=mImageCaptureUri;*/
        }
        if(selectedImage==null)
        {
            Log.d("Main Activity","Back");
            return;
        }



    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("Main Activity","Exception");
        return;
    }      
    return;

}

selectedImage 是捕获图像的 Uri,它在 Sony 手机上运行良好。

【讨论】:

    【解决方案2】:

    这里,您没有在tempFile 中提供任何图像名称,因此拍摄的图像可以使用该名称保存在 SDCard 中。

    现在,比如说,图像名称将是123.jpg....替换它

    File tempFile = new File(Environment.getExternalStorageDirectory().getPath()+"/ABC";);
    

    与...

    File tempFile = new File(Environment.getExternalStorageDirectory().getPath()+"/ABC/123.jpg");
    

    【讨论】:

    • 我已经按照您的描述传递了这些变量。请参阅编辑后的 ​​SO。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多