我正在使用以下代码在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 null。 如何解决这个问题,它只在HTC设备中。
答案 0 :(得分:2)
当我试图用相机拍摄图像时,我的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;
答案 1 :(得分:0)
对于HTC Desire HD,以下方法有效。
protected Uri createUriFromPhotoIntentForHtcDesireHD( Intent intent, Uri uri ) {
FileOutputStream fos = null;
try {
Bitmap bitmap = (Bitmap) intent.getExtras().get( "data" );
File outputDir = getCacheDir();
File outputFile = File.createTempFile( "Photo-", ".jpg", outputDir );
fos = new FileOutputStream( outputFile );
bitmap.compress( Bitmap.CompressFormat.JPEG, 90, fos );
uri = Uri.fromFile( outputFile );
} catch ( IOException e ) {
Ln.e( e, "Error creating temp file for HTC Desire HD" );
e.printStackTrace();
} finally {
try {
if ( fos != null ) {
fos.close();
}
} catch ( IOException e ) {
Ln.e( e, "Error closing temp file for HTC Desire HD" );
}
}
return uri;
}