我有这段代码,我试图从相机中获取图像。我不断收到“:无法打开文件进行阅读”。它将文件保存在指定的目录中。我遇到的另一个问题是
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)