我正在查看一些触发android.media.action.IMAGE_CAPTURE
意图的示例,并使用类级变量来存储生成的图像。我不想那样做。我想我应该能够为目标提供文件的URI,然后在完成时从意图中获取该URI。我想这样做:
void snapPixButton_Click(object sender, EventArgs e)
{
Intent cameraIntent = new Intent(MediaStore.ActionImageCapture);
File file = new File(Home.SnapStorageLocation, string.Format("{0}.jpg", Guid.NewGuid()));
cameraIntent.PutExtra(MediaStore.ExtraOutput, file.ToURI().ToString());
StartActivityForResult(cameraIntent, SnapPixIntentRequestCode);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == SnapPixIntentRequestCode && resultCode == Result.Ok)
{
URI uri = new URI(data.GetStringExtra(MediaStore.ExtraOutput));
File file = new File(uri);
using (Bitmap bitmap = LoadAndResizeBitmap(file.Path, this.ImageView.Width, this.ImageView.Height))
{
this.ImageView.SetImageBitmap(bitmap);
}
}
}
但在OnActivityResult
中,对data.GetStringExtra(MediaStore.ExtraOutput)
的调用会产生以下消息:未知标识符:MediaStore
我做错了什么?我不认为我应该保持类级变量,我应该能够将数据传递给一个intent然后在之后提取它,对吗?
答案 0 :(得分:0)
我以这种方式检索Uri
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Uri imageFilePathUri = null;
if (cameraImagePath != null) {
Cursor imageCursor = getContentResolver().query(
cameraImagePath, filePathColumn, null, null, null);
if (imageCursor != null && imageCursor.moveToFirst()) {
int columnIndex = imageCursor.getColumnIndex(filePathColumn[0]);
String filePath = imageCursor.getString(columnIndex);
imageCursor.close();
imageFilePathUri = filePath != null ? Uri
.parse(filePath) : null;
}
并以此Intent开始
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues(3);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
cameraImagePath = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath);
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
答案 1 :(得分:0)
我这样做的方法是使用新的File对象。在我拍照之前,我创建了一个新文件,因此我知道要使用哪个文件。
启动Intent的代码:
var cameraIntent = new Intent(MediaStore.ActionImageCapture);
var availableActivies = PackageManager.QueryIntentActivities(cameraIntent,
PackageInfoFlags.Permissions);
if (availableActivies != null && availableActivies.Count > 0)
{
var dir =
new File(
Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryPictures), "Map name");
if (!dir.Exists())
{
dir.Mkdirs();
}
_imageFile = new File(dir, string.Format("CP2{0}.jpg", DateTime.Now.Ticks));
cameraIntent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(_imageFile));
StartActivityForResult(cameraIntent,100);
}
然后在活动结果上我就这样做了:
using (Bitmap usedBitmap = BitmapFactory.DecodeFile(_imageFile.AbsolutePath))
{
_usedImageView = FindViewById<ImageView>(Resource.Id.imageView1);
_usedImageView.SetImageBitmap(usedBitmap);
_usedImageView.Dispose();
usedBitmap.Dispose();
}
希望这有助于解决您的问题。