我正在使用以下代码在 Android 中捕获图像:
String fileName = "image.jpeg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image captured by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
捕获图像后,我使用以下代码获取图像:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
if(resultCode == Activity.RESULT_OK)
{
if(imageUri == null)
imageUri = data.getData();
String filePath = getPath(imageUri);
UploadContentToServer(filePath, "image.jpeg");
//getBytesFromFile(filePath);
}
}
}
它在除 HTC 横屏模式外的所有设备上都能正常工作。
我在我的应用中使用纵向模式。但是当我在我的 HTC 中以横向模式拍摄图像时,我得到的 imageUri 为空。 如何解决此问题,仅在 HTC 设备中。
最佳答案
尝试使用相机拍摄图像时,我在 Galaxy s2 上遇到了同样的问题。尝试在 Intent 中指定真实路径,然后在您的 onActivityResult 中获取路径。直接从 Intent 数据中提取 uri 对我来说在图像捕获中也不起作用。
这是我的意思的一个例子:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
fileName = dateFormat.format(new Date()) + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(), fileName);
Intent cameraintent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageURICamera = Uri.fromFile(photo);
startActivityForResult(cameraintent, 1);
现在在 onActivityResult 中再次获取照片:
realPath = Environment.getExternalStorageDirectory() + "/" + fileName;
关于android - HTC Mobile 中的图像捕获问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819777/