2

我正在使用以下代码在 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 设备中。

4

2 回答 2

2

尝试使用相机拍摄图像时,我在我的 Galaxy s2 上遇到了同样的问题。尝试在意图中指定真实路径,然后在 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; 
于 2011-07-26T14:15:42.800 回答
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;
}
于 2012-09-05T16:38:23.310 回答