我使用 Intent 和 ACTION_IMAGE_CAPTURE 使用相机拍照,我成功获取了照片的 uri,但是当我使用此 uri 获取路径时,发生了异常。当我去画廊时,我没有看到照片。我重新启动手机,然后我在图库中看到了这张照片。一旦我完成意图,谁能告诉我如何获取路径或让位图进入画廊?
我的代码:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Date today = new Date();
fileName = Integer.toString(today.getYear()) + Integer.toString(today.getMonth()) + Integer.toString(today.getDay()) +
Integer.toString(today.getHours()) + Integer.toString(today.getMinutes()) + Integer.toString(today.getSeconds());
File out = new File(Environment.getExternalStorageDirectory(), fileName + ".jpg");
tempUri = Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
startActivityForResult(intent, TAKEPHOTO);
break;
并在 onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == COMPLETE)
{
if (data != null)
{
Uri uri = data.getData();
if (uri != null)
{
uris[currentIndex] = uri;
setBitmapFromUri();
}
}
}
else if (requestCode == TAKEPHOTO)
{
uris[currentIndex] = tempUri;
setBitmapFromUri();
}
super.onActivityResult(requestCode, resultCode, data);
}
这是 setBitmapFromUri:
private void setBitmapFromUri()
{
Uri uri = uris[currentIndex];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 3;
try
{
String path = getAbsoluteImagePath(uri);
ExifInterface exif = new ExifInterface(path);
int tag = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
int degree = 0;
if (tag == ExifInterface.ORIENTATION_ROTATE_90)
{
degree = 90;
}
else if (tag == ExifInterface.ORIENTATION_ROTATE_180)
{
degree = 180;
}
else if (tag == ExifInterface.ORIENTATION_ROTATE_270)
{
degree = 270;
}
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
Matrix matrix = new Matrix();
matrix.setScale (2 * getWindowManager().getDefaultDisplay().getWidth() / 3 / bitmap.getWidth(), 2 * getWindowManager().getDefaultDisplay().getHeight() / 3 / bitmap.getHeight());
matrix.setRotate(degree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageViews.get(currentIndex).setImageBitmap(bitmap);
} catch (IOException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
这是一个例外:
05-18 10:38:20.645: ERROR/AndroidRuntime(3342): FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.Trine.HideAndSeek/com.Trine.HideAndSeek.UploadHints}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2653)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2695)
at android.app.ActivityThread.access$2000(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.Trine.HideAndSeek.UploadHints.getAbsoluteImagePath(UploadHints.java:324)
at com.Trine.HideAndSeek.UploadHints.setBitmapFromUri(UploadHints.java:140)
at com.Trine.HideAndSeek.UploadHints.onActivityResult(UploadHints.java:196)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2649)