这是我的问题:我需要将照片保存到默认的相机位置,而不是将照片复制到我的私人位置,添加GPS标签进行复制,以及删除原始照片。为何如此?因为在某些设备上,在请求中使用MediaStore.EXTRA_OUTPUT
时,照片仍保存在默认位置。现在,当应用程序接近完成时,在新设备(摩托罗拉MB526)上进行测试时,它会在接受拍摄照片后崩溃。同样的事情发生在模拟器上。
问题是,onActivityResult(int requestCode, int resultCode, Intent data)
data.getData()
中的内容返回null。
所以我的问题是 - 是否有任何统一的方法可以在给定的位置保存图片而不是其他地方?
修改
捕捉图像意图
protected void takePhotos() {
Intent imageIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
和结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK && data != null) {
Uri capturedImageUri = data.getData();
Log.d("cameraResult", "data =" + data.getDataString());
String capturedPicFilePath = getRealPathFromURI(capturedImageUri);
writeImageData(capturedImageUri, capturedPicFilePath);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture not taken",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture not taken",
Toast.LENGTH_SHORT).show();
}
}
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] projx = { MediaStore.Images.Media.DATA };
Cursor cursor;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
CursorLoader loader = new CursorLoader(this, contentUri, projx,
null, null, null);
cursor = loader.loadInBackground();
} else {
cursor = this.managedQuery(contentUri, projx, null, null, null);
}
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
logCat输出:
07-23 18:42:26.486: D/cameraResult(1744): data =null
07-23 18:42:26.486: D/AndroidRuntime(1744): Shutting down VM
07-23 18:42:26.486: W/dalvikvm(1744): threadid=1: thread exiting with uncaught exception (group=0xb70334f0)
07-23 18:42:26.496: E/AndroidRuntime(1744): FATAL EXCEPTION: main
07-23 18:42:26.496: E/AndroidRuntime(1744): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1513, result=-1, data=Intent { act=inline-data (has extras) }} to activity {arios.e_gps/arios.e_gps.photos.PhotoListAct}: java.lang.NullPointerException
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.os.Looper.loop(Looper.java:130)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-23 18:42:26.496: E/AndroidRuntime(1744): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 18:42:26.496: E/AndroidRuntime(1744): at java.lang.reflect.Method.invoke(Method.java:507)
07-23 18:42:26.496: E/AndroidRuntime(1744): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-23 18:42:26.496: E/AndroidRuntime(1744): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-23 18:42:26.496: E/AndroidRuntime(1744): at dalvik.system.NativeStart.main(Native Method)
07-23 18:42:26.496: E/AndroidRuntime(1744): Caused by: java.lang.NullPointerException
07-23 18:42:26.496: E/AndroidRuntime(1744): at arios.e_gps.photos.PhotoListAct.getRealPathFromURI(PhotoListAct.java:316)
07-23 18:42:26.496: E/AndroidRuntime(1744): at arios.e_gps.photos.PhotoListAct.onActivityResult(PhotoListAct.java:165)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
07-23 18:42:26.496: E/AndroidRuntime(1744): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
07-23 18:42:26.496: E/AndroidRuntime(1744): ... 11 more
答案 0 :(得分:3)
Data.getData()不为null。将图片录制到任何位置时,它始终为空。
答案 1 :(得分:0)
修改
这可能是错的,但我在ACTION_IMAGE_CAPTURE之后做了这个以获得uri,它对我有用。
Bitmap picture = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.JPEG, 100, stream);
String path = Images.Media.insertImage(getContentResolver(), picture,
"title" , null);
Uri uriPhoto = Uri.parse(path);