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)