我看到了另一个关于这个问题的问题,但是他们没有帮助。
在我的应用程序中,有两种不同的方法可以添加图像并在imageview中显示它。 1.画廊 - 2.相机。
画廊方式工作正常。
但是以Camera方式(我用原生的Android相机做)我有一个令人困惑的问题。
如果我将手机旋转到纵向模式(因此肖像xml加载)并以纵向模式拍摄照片,它可以正常工作。
如果我将它旋转到横向并在风景中也使用相机,它也可以正常工作。
现在,如果我的xml是纵向的,并且在进入相机后我将其旋转为横向(拍摄lansdcape照片)应用程序崩溃并给我这个错误:
05-08 21:34:22.974: E/AndroidRuntime(19000): FATAL EXCEPTION: main
05-08 21:34:22.974: E/AndroidRuntime(19000): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.kpaxteam.babyalbum/com.tiktak.albums1.A2}: java.lang.NullPointerException
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.app.ActivityThread.deliverResults(ActivityThread.java:2918)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2970)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.app.ActivityThread.access$2000(ActivityThread.java:132)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1068)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.os.Looper.loop(Looper.java:150)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.app.ActivityThread.main(ActivityThread.java:4277)
05-08 21:34:22.974: E/AndroidRuntime(19000): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 21:34:22.974: E/AndroidRuntime(19000): at java.lang.reflect.Method.invoke(Method.java:507)
05-08 21:34:22.974: E/AndroidRuntime(19000): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-08 21:34:22.974: E/AndroidRuntime(19000): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-08 21:34:22.974: E/AndroidRuntime(19000): at dalvik.system.NativeStart.main(Native Method)
05-08 21:34:22.974: E/AndroidRuntime(19000): Caused by: java.lang.NullPointerException
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.content.ContentResolver.acquireProvider(ContentResolver.java:745)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.content.ContentResolver.query(ContentResolver.java:258)
05-08 21:34:22.974: E/AndroidRuntime(19000): at com.tiktak.babyalbum.Helper.pathgal(Helper.java:161)
05-08 21:34:22.974: E/AndroidRuntime(19000): at com.tiktak.babyalbum.Helper.resultpic(Helper.java:299)
05-08 21:34:22.974: E/AndroidRuntime(19000): at com.tiktak.albums1.A2.onActivityResult(A2.java:242)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.app.Activity.dispatchActivityResult(Activity.java:4053)
05-08 21:34:22.974: E/AndroidRuntime(19000): at android.app.ActivityThread.deliverResults(ActivityThread.java:2914)
我的拍照意图:
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
act.startActivityForResult(cameraIntent, int2);
我的ActivityResult:
else if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
Helper line 299---> String capturedImageFilePath = Helper.pathgal(act, mCapturedImageURI);
bmp = Helper.decodeFile(capturedImageFilePath, act);
if(bmp != null )
{
try {
Helper.showpic(act, id1, bmp);
settings = act.getSharedPreferences(st1, 0);
settings.edit().putString(st2, capturedImageFilePath).putInt(st3, View.VISIBLE).putInt(st4, View.GONE).commit();;
img1 = (ImageView) act.findViewById(id1);
img2 = (ImageView) act.findViewById(id2);
img1.setVisibility(View.VISIBLE);
img2.setVisibility(View.GONE);
} catch (OutOfMemoryError e) {
Toast.makeText(act.getApplicationContext(), "Try Again",
Toast.LENGTH_LONG).show();
}
}
获取图像文件路径代码:
public static String pathgal(Activity act, Uri uri){
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Helper 161---> Cursor cursor = act.getContentResolver().query(uri, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
}
return null;
}
任何想法?
答案 0 :(得分:0)
使用以下代码
我有同样的问题解决了吗?
private void dispatchTakePictureIntent() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
name=mode+"_"+img_type+"_"+timeStamp+".jpeg";
output = new File(dir,name);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
startActivityForResult(cameraIntent, 1);
}
在onActivityResult中使用output.getAbsolutePath()(其中output是我们用来创建uri以传递相机意图的文件))来获取该图像
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
String IMAGE_URL=output.getAbsolutePath();
}
答案 1 :(得分:0)
我应该只添加android:configChanges =" orientation"在我的活动中。
谢谢Everyvone。