2013-03-09 77 views
1

我有這段代碼,我試圖從相機獲取圖像。我不斷收到「:無法打開文件閱讀」。它將文件保存在指定的目錄中。 我得到的另一個問題是相機意圖:無法恢復致命異常,不存儲

public class MainActivity extends Activity { 
private Uri imageUri; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

} 


public void takePic(View view){ 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
    imagesFolder.mkdirs(); // <---- 
    File photo = new File(imagesFolder, "image_001.jpg"); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      Uri.fromFile(photo)); 
    imageUri = Uri.fromFile(photo); 
    startActivityForResult(intent, 100); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case 100: 
     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImage = imageUri; 
      getContentResolver().notifyChange(selectedImage, null); 
      ImageView imageView = (ImageView) findViewById(R.id.iv_photo); 
      ContentResolver cr = getContentResolver(); 
      Bitmap bitmap; 
      try { 
       bitmap = android.provider.MediaStore.Images.Media 
       .getBitmap(cr, selectedImage); 

       imageView.setImageBitmap(bitmap); 
       Toast.makeText(this, selectedImage.toString(), 
         Toast.LENGTH_LONG).show(); 
      } catch (Exception e) { 
       Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
         .show(); 
       Log.e("Camera", e.toString()); 
      } 
     } 
    } 

} 


public void doCrop(){ 
    Intent intent = new Intent("com.android.camera.action.CROP"); 
    intent.setType("image/*"); 
    intent.setData(imageUri); 
    Log.d("Gothere", "awesome"); 
    intent.putExtra("outputX", 200); 
    intent.putExtra("outputY", 200); 
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    intent.putExtra("scale", true); 
    intent.putExtra("return-data", true); 
    Log.d("Gothere", "awesome2"); 
    startActivityForResult(intent, 1333); 
} 

}

03-09 08:20:27.456: E/AndroidRuntime(18776): FATAL EXCEPTION: main 
    03-09 08:20:27.456: E/AndroidRuntime(18776): java.lang.RuntimeException: Unable to resume   activity {com.example.cameracode/com.example.cameracode.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.cameracode/com.example.cameracode.MainActivity}: java.lang.NullPointerException 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2617) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2645) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2118) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3554) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.access$800(ActivityThread.java:139) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1230) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.os.Looper.loop(Looper.java:137) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.main(ActivityThread.java:4918) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at java.lang.reflect.Method.invokeNative(Native Method) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at java.lang.reflect.Method.invoke(Method.java:511) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at dalvik.system.NativeStart.main(Native Method) 
03-09 08:20:27.456: E/AndroidRuntime(18776): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.cameracode/com.example.cameracode.MainActivity}: java.lang.NullPointerException 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.deliverResults(ActivityThread.java:3183) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2604) 
03-09 08:20:27.456: E/AndroidRuntime(18776): ... 13 more 
03-09 08:20:27.456: E/AndroidRuntime(18776): Caused by: java.lang.NullPointerException 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.os.Parcel.readException(Parcel.java:1431) 
03-09 08:20:27.456: E/AndroidRuntime(18776): at android.os.Parcel.readException(Parcel.java:1379) 

回答

0

也許嘗試註釋掉這一行:

getContentResolver().notifyChange(selectedImage, null); 

還檢查了這一點,你裝在從相機拍攝的圖像全分辨率,巫婆是足夠的內存大約1或2位圖在您的應用程序,與此代碼,您可以在不加載到內存的情況下對圖像進行採樣,因此,當您將其加載到內存中時,噸大小合理

String selectedImagePath = selectedImage.getAbsolutePath(); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 
       BitmapFactory.decodeFile(selectedImagePath, options); 
       int imageHeight = options.outHeight; 
       int imageWidth = options.outWidth; 
       String imageType = options.outMimeType; 
       if(imageWidth > imageHeight){ 
        options.inSampleSize = calculateInSampleSize(options,512,256);//<------the size you need if landscape 
       }else{ 
        options.inSampleSize = calculateInSampleSize(options,256,512);//<------the size you need if portrait 
       } 
       options.inJustDecodeBounds = false; 
       bitmap = BitmapFactory.decodeFile(selectedImagePath,options); 

這是方法

public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
// Raw height and width of image 
final int height = options.outHeight; 
final int width = options.outWidth; 
int inSampleSize = 1; 

if (height > reqHeight || width > reqWidth) { 

    // Calculate ratios of height and width to requested height and width 
    final int heightRatio = Math.round((float) height/(float) reqHeight); 
    final int widthRatio = Math.round((float) width/(float) reqWidth); 

    // Choose the smallest ratio as inSampleSize value, this will guarantee 
    // a final image with both dimensions larger than or equal to the 
    // requested height and width. 
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
} 

return inSampleSize; 
}