2014-11-23 58 views
-1

我想启动摄像头并捕获图像文件在不活动的结果,然后将其保存在文件中..但我不断收到java.lang.NullPointerException在getContentResolver.notifyChange线。安卓空指针异常与摄像头图像

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) { 
     case 1001: 
      if(resultCode == Activity.RESULT_OK) { 
       getContentResolver().notifyChange(imageUri, null); 
       imageView = (ImageView)findViewById(R.id.take_photo_image_view); 
       ContentResolver contentResolver = getContentResolver(); 
       Bitmap bitmap; 
       try { 
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(contentResolver, 
         imageUri); 
        imageView.setImageBitmap(bitmap); 
       } catch(Exception e) { 
        Toast.makeText(GetPhotoActivity.this, "failed to load", 
        Toast.LENGTH_LONG).show(); 
        Log.e(LOG_TAG, e.toString()); 
       } 
      } 
    } 
} 

11-23 17:26:00.508 32727-32727/com.example.testspotter E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.testspotter, PID: 32727 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=null} to activity {com.example.testspotter/com.example.testspotter.GetPhotoActivity}: java.lang.NullPointerException 
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3942) 
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992) 
     at android.app.ActivityThread.access$1300(ActivityThread.java:156) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:157) 
     at android.app.ActivityThread.main(ActivityThread.java:5872) 
     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:852) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
     at android.os.Parcel.readException(Parcel.java:1480) 
     at android.os.Parcel.readException(Parcel.java:1428) 
     at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:486) 
     at android.content.ContentResolver.notifyChange(ContentResolver.java:1668) 
     at android.content.ContentResolver.notifyChange(ContentResolver.java:1657) 
     at android.content.ContentResolver.notifyChange(ContentResolver.java:1637) 
     at com.example.testspotter.GetPhotoActivity.onActivityResult(GetPhotoActivity.java:126) 
+2

您应该粘贴完整的例外信息,以便我们先帮助 – shanwu 2014-11-23 23:45:11

+0

随意从日志中发布堆栈跟踪,这表明异常来自哪条线。然后,阅读https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html,了解它为什么会在该行上发生的提示。然后,回到这里。 – 2014-11-23 23:45:43

+0

你能指出哪一行粘贴在这里126对应吗? – mobilepotato7 2014-11-24 00:37:38

回答

0

来电相机活性使用以下代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
 
        File f = new File(android.os.Environment 
 
          .getExternalStorageDirectory(), "temp.jpg"); 
 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
 
        startActivityForResult(intent, REQUEST_CAMERA);

在onActivityResult

if (resultCode == RESULT_OK) { 
 
      if (requestCode == REQUEST_CAMERA) { 
 
       File f = new File(Environment.getExternalStorageDirectory() 
 
         .toString()); 
 
       for (File temp : f.listFiles()) { 
 
        if (temp.getName().equals("temp.jpg")) { 
 
         f = temp; 
 
         break; 
 
        } 
 
       } 
 
       try { 
 
        Bitmap bm; 
 
        BitmapFactory.Options btmapOptions = new BitmapFactory.Options(); 
 
        imageView = (ImageView)findViewById(R.id.take_photo_image_view); 
 
        bm = BitmapFactory.decodeFile(f.getAbsolutePath(), 
 
          btmapOptions); 
 
    
 
        // bm = Bitmap.createScaledBitmap(bm, 70, 70, true); 
 
        imageView.setImageBitmap(bm); 
 
    
 
        String path = android.os.Environment 
 
          .getExternalStorageDirectory() 
 
          + File.separator 
 
          + "Phoenix" + File.separator + "default"; 
 
        f.delete(); 
 
        OutputStream fOut = null; 
 
        File file = new File(path, String.valueOf(System 
 
          .currentTimeMillis()) + ".jpg"); 
 
        try { 
 
         fOut = new FileOutputStream(file); 
 
         bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
 
         fOut.flush(); 
 
         fOut.close(); 
 
        } catch (FileNotFoundException e) { 
 
         e.printStackTrace(); 
 
        } catch (IOException e) { 
 
         e.printStackTrace(); 
 
        } catch (Exception e) { 
 
         e.printStackTrace(); 
 
        } 
 
       } catch (Exception e) { 
 
        e.printStackTrace(); 
 
       } 
 
      }