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)
4

1 回答 1

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;
}
于 2013-03-09T17:11:37.597 回答