我正在开发一个应用程序,可以拍摄照片并将其保存到SD卡上的目录中。它在大多数设备上运行良好,但我在很少的设备上出现此错误:
08-26 15:29:11.712 11925 11925 E AndroidRuntime: FATAL EXCEPTION: main
08-26 15:29:11.712 11925 11925 E AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data dat=file:///storage/emulated/0/.MyImgs/IMG_20132926032905.jpg typ=image/jpeg (has extras) }} to activity {com.example.myApp/com.example.myApp.Com}: java.lang.NullPointerException
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3403)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3446)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.access$1100(ActivityThread.java:150)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Looper.loop(Looper.java:213)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5153)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: Caused by: java.lang.NullPointerException
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1445)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1389)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:472)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.ContentResolver.notifyChange(ContentResolver.java:1297)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.ContentResolver.notifyChange(ContentResolver.java:1286)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.ContentResolver.notifyChange(ContentResolver.java:1266)
-->08-26 15:29:11.712 11925 11925 E AndroidRuntime: at com.example.myApp.Com.onActivityResult(Com.java:265)<--
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.Activity.dispatchActivityResult(Activity.java:5293)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3399)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: ... 11 more
以下是我创建文件夹,保存图像和启动相机的方法
String root = Environment.getExternalStorageDirectory()+"/.MyImgs/";
File newDirectory = new File(root);
//create directory if it doesn't exist
if(!newDirectory.exists())
newDirectory.mkdirs();
//create and name the image
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
File f = new File(newDirectory,"IMG_"+date+".jpg");
//save image directory
mUri = Uri.fromFile(f);
// Result of activity: TAKE_PICTURE
startActivityForResult(i, TAKE_PICTURE);
以下是发生错误的部分:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(mUri, null);//line 265
}
}
任何人都可以帮我找到错误吗?为什么它适用于某些设备但不适用于其他设备?感谢。
答案 0 :(得分:7)
试试这个,它对我来说就像魅力一样:
private String selectedImagePath = "";
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
btnGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
}
});
btnCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == PICK_IMAGE) {
selectedImagePath = getAbsolutePath(data.getData());
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else if (requestCode == CAPTURE_IMAGE) {
selectedImagePath = getImagePath();
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}