ALL,检索摄像机图像上的Android
我有以下代码:
try
{
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
cameraImageFiles = File.createTempFile("user_photo", ".png", storage);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraImageFiles));
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent photoIntent = Intent.createChooser(galleryIntent, "Select or take a new picture");
photoIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent });
startActivityForResult(photoIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
catch(IOException e)
{
Utils.displayErrorDialog(context, e.getMessage());
}
在网络上的每一个例子,在这里谈谈从摄像机获取图像时,只有1意图。
在这里,我有一个IntentChooser来选择图库图像或使用相机拍摄新照片。据我了解使用这段代码,我不能简单地获取图像,因为在“onActivityResult()”我应该将图片保存到文件 - 它不会自动保存。
现在我想要做的就是从相机拍摄图像。我真的不在乎它是否会被保存 - 我只是想要一张照片。
我知道如何从图库中获取图像 - 我有这个代码,它的工作原理。但从相机获取图像现在是一个难题。
谢谢。
[编辑] 这是我放在onActivityResult()的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
Uri uri = null;
if(data == null)
{
if(cameraImageFiles.exists())
{
uri = Uri.fromFile(cameraImageFiles);
InputStream input;
try
{
input = getContentResolver().openInputStream(uri);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream(input, null, opts);
input.close();
int height = opts.outHeight;
int width = opts.outWidth;
int inSampleSize = 1;
int reqHeight = camera.getHeight();
int reqWidth = camera.getWidth();
if(height > reqHeight || width > reqWidth)
{
int halfHeight = height/2;
int halfWidth = width/2;
while((halfHeight/inSampleSize) > reqHeight && (halfWidth/inSampleSize) > reqWidth)
inSampleSize *= 2;
}
opts.inSampleSize = inSampleSize;
opts.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(input, null, opts);
camera.setImageBitmap(bmp);
}
catch(FileNotFoundException e)
{
Utils.displayErrorDialog(this, e.getMessage());
}
catch(IOException e)
{
Utils.displayErrorDialog(this, e.getMessage());
}
}
photoReady = true;
}
执行后,代码给出空为bmp,所以我想不保存图像。
[/编辑]
它被保存。否则无法获得照片。 – CommonsWare
您需要使用SurfaceView创建自己的相机。 – njzk2
为什么有意在画廊或照相机之间进行选择?这没有任何意义,为什么你没有提出一个对话框,并在对话结果 – Tascalator