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 に答える