[英]Camera Intent: Unable to resume Fatal Exception and does not store
I have this piece of code where I am trying to get image from camera. 我有这段代码试图从相机中获取图像。 I keep getting ": Can't open file for reading".
我不断收到“:无法打开文件进行读取”。 It saves the file in the specified directory though.
但是它将文件保存在指定目录中。 Another problem I get is
我遇到的另一个问题是
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)
maybe try to comment out this line: 也许尝试注释掉这一行:
getContentResolver().notifyChange(selectedImage, null);
also check this out, your loading the picture taken from the camera at full resolution, witch is enough memory for about 1 or 2 bitmaps in your app, with this code you can sample the image without loading it into memory, so when you do load it into memory its at a reasonable size 还要检查一下,以全分辨率加载从相机拍摄的照片,女巫在应用程序中有足够的内存来存储大约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);
this is method 这是方法
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.