我使用Intent和ACTION_IMAGE_CAPTURE来使用相机并拍照,我成功地获得了照片的uri,但是当我使用这个uri获取路径时,发生了异常。当我去画廊时,我没有看到照片。我重新启动手机,然后在图库中看到照片。任何人都可以告诉我如何在完成意图后立即获取路径或让位图进入图库?
MyCodes:
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)
答案 0 :(得分:1)
我用这个从图像的Uri获取真实的文件路径..
//----------------------------------------
/**
* This method is used to get real path of file from from uri
*
* @param contentUri
* @return String
*/
//----------------------------------------
public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
此外,要在Gallery中查看捕获的图像,您必须从应用程序启动Android Media Scanner。
在拍摄图像后尝试此行..
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
答案 1 :(得分:1)
试试这个,它对我有用:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"temp_file_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try
{
intent.putExtra("return-data", true);
startActivityForResult(intent, INTENT_FOR_IMG_PROCESSING);
}
catch (ActivityNotFoundException e)
{
e.printStackTrace();
}