显示从相机拍摄的图像时,Uri 为空
Posted
技术标签:
【中文标题】显示从相机拍摄的图像时,Uri 为空【英文标题】:Uri is null while displaying image taken from camera 【发布时间】:2014-01-18 12:07:48 【问题描述】:您好,我需要从相机拍照并在屏幕上显示。
但我将 Uri 设为空。
我需要获取文件的绝对路径。
我的代码:
mintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mintent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
startActivityForResult(mintent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if ((data != null && !data.toString().equalsIgnoreCase("Intent "))
|| requestCode == 1)
switch (requestCode)
case 1:
try
Uri imageFileUri = data.getData();
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
img2.setImageBitmap(bitmap);
请帮忙..
【问题讨论】:
【参考方案1】:你的问题在这里:
mintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mintent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
您没有传递希望文件驻留的 URI。相反,您传递的是文件夹的 URI。相反,您应该将 URI 传递到您希望文件转到的位置。这是我几个月前做的一个项目的一个例子:
File lastSavedFile;
/**
* IMPORTANT: this must be a directory readable by multiple apps (not private storage)
* @return
*/
@SuppressLint("SimpleDateFormat")
private File getTempFile()
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "barfile_" + timeStamp + ".jpg";
return new File(Environment.getExternalStorageDirectory(), imageFileName);
/**
* Called when we want to take a picture
*
* @param position
*/
private void launchTakePictureIntent(int position)
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
lastSavedFile = getTempFile();
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(lastSavedFile));
startActivityForResult(i, position);
/**
* Returns from the camera intent, hopefully with a picture
*/
@Override
protected void onActivityResult(int position, int resultCode, Intent intent)
super.onActivityResult(position, resultCode, intent);
if (resultCode == RESULT_OK)
Uri imageUri = Uri.fromFile(lastSavedFile);
Bitmap fullBitmap;
try
fullBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
catch (Exception e)
Log.e(TAG, e.toString(), e);
return;
...
【讨论】:
【参考方案2】:这是我用来获取图片 Uri 的代码。
private Bitmap photo;
private File pic;
private Uri uri;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == 1)
photo = (Bitmap) data.getExtras().get("data");
try
File root = Environment.getExternalStorageDirectory();
if (root.canWrite())
pic = new File(root, "pic.png");
FileOutputStream out = new FileOutputStream(pic);
photo.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
catch (IOException e)
Log.e("BROKEN", "Could not write file " + e.getMessage());
// This is the uri you are looking for.
uri = Uri.fromFile(pic);
PS:如果您不想声誉不佳,请不要忘记接受上一个问题中的答案:onActivityResult is not calling after taking camera picture。
【讨论】:
以上是关于显示从相机拍摄的图像时,Uri 为空的主要内容,如果未能解决你的问题,请参考以下文章