2016-01-12 24 views
1

錯誤:的NullPointerException在附加圖片庫

FATAL EXCEPTION: main
java.lang.NullPointerException at java.io.File.fixSlashes(File.java:185)
at java.io.File.(File.java:134) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.galleryAddPic(DeliverActivity.java:308) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.onClick(DeliverActivity.java:246) at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

代碼:這條線

private void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent("android.media.action.IMAGE_CAPTURE"); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
      } catch (IOException ex) { 
       Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show(); 
      } 
      if (photoFile != null) { 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
       startActivityForResult(takePictureIntent, 0); 
      } 
     } 
    } 

private File createImageFile() throws IOException { 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Prokaton/Big"); 
     File image = File.createTempFile(imageFileName, ".jpg", storageDir); 

     mCurrentPhotoPath = image.getAbsolutePath(); 
     imageName = image.getName(); 
     ... 
     return image; 
    } 

private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
} 

錯誤: 文件f =新的文件(mCurrentPhotoPath);

Android 4.0.3上的錯誤。在其他版本工作正常。

+0

有一個在你的文件路徑 –

+1

斜線一些問題的錯誤清楚地表明,'mCurrentPhotoPath'是空,因此您需要在調用'galleryAddPic()'方法之前爲其指定一個非空值。 – Saheed

回答

0

mCurrentPhotoPath在您的代碼中爲空,因此請提供完整的代碼來檢查此錯誤。 enter image description here

image.getabsolutePath();在您的代碼中爲null。

+0

我提供了完整的代碼,請看看 – user3712384

+1

好的,thx,但爲什麼image.getabsolutePath()null?正如我所說的,這段代碼在antother android版本上工作得很完美。但在4.0.3上,我發現錯誤。 – user3712384

+0

在4.0.3過程中被更改爲獲取路徑,這就是爲什麼它變爲空。如果你同意這個答案,然後支持這個答案 –

1

根據您發佈的代碼,我的猜測是,你經歷了之前dispatchTakePictureIntent()錯誤獲取調用,因爲它是這個方法調用createImageFile()方法,設置與非空值的mCurrentPhotoPath變量。

一個簡單的解決將是一個不空檢查,像這樣以更新galleryAddPic()方法:

private void galleryAddPic() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     if (mCurrentPhotoPath == null) { 
      return; 
     } 
     File f = new File(mCurrentPhotoPath); 
     Uri contentUri = Uri.fromFile(f); 
     mediaScanIntent.setData(contentUri); 
     this.sendBroadcast(mediaScanIntent); 
    } 
+0

正如我所說,這個代碼在antother android版本上工作得很完美。添加你的「如果」和現在的代碼捕獲異常。 – user3712384