2

我正在使用以下代码从相机或其他意图中获取图像。在我的三星 S2 上它工作正常,但 HTC 给出错误

private void openImageIntent() 
{
    // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "OpenionDroid" + File.separator);
    root.mkdirs();
    final String fname = UUID.randomUUID().toString(); 
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

     // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = mContext.getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for(ResolveInfo res : listCam) 
    {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
     }

     // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
        // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    startActivityForResult(chooserIntent, Constants.IMAGE_REQUEST_CODE);
}

@SuppressWarnings("static-access")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == mContext.RESULT_OK)
    {
       if(requestCode == Constants.IMAGE_REQUEST_CODE)
       {
           final boolean isCamera;
           if(data == null)
           {
              isCamera = true;
           }
           else
           {
              final String action = data.getAction();
              if(action == null)
              {
                  isCamera = false;
              }
              else
              {
                  isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
              }
           }

           Uri selectedImageUri;
           if(isCamera)
              selectedImageUri = outputFileUri;
           else
              selectedImageUri = data == null ? null : data.getData();

           if (isPhoto1Selected)
           {
               photo1Uri = selectedImageUri;
               try {
                    Bitmap bitmap = Utilities.decodeSampledBitmap(Utilities.readBytes(selectedImageUri, mContext), 100, 100);
                    myQuestion.setLeftThumbnail(Utilities.decodeBitmapToArray(bitmap));
                    photo1.setImageBitmap(bitmap);
               } catch (IOException e) {
                   Utilities.displayAlert("Oops!", e.getMessage(), mContext);
                   e.printStackTrace();
               }
           }
           else
           {
               photo2Uri = selectedImageUri;
               try {
                    Bitmap bitmap = Utilities.decodeSampledBitmap(Utilities.readBytes(selectedImageUri, mContext), 100, 100)
                            ;
                    myQuestion.setRightThumbnail(Utilities.decodeBitmapToArray(bitmap));
                    photo2.setImageBitmap(bitmap);
               } catch (IOException e) {
                   Utilities.displayAlert("Oops!", e.getMessage(), mContext);
                   e.printStackTrace();
               }
           }
        }
     }
}

在我的代码 S2 中输入

if(data == null)
           {
              isCamera = true;
           }

虽然 HTC 去这里

  isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

结果 isCamera 为假。所以它试图运行

selectedImageUri = data == null ? null : data.getData();

这给出了 null

什么可能是解决方案,我如何从相机中检索图像?

4

1 回答 1

2

对于 HTC Desire,您需要对拍照进行一些特殊处理。这是一些代码,您必须根据需要对其进行调整:

 protected static Uri createUriFromPhotoIntentForHtcDesireHD( Activity activity, Intent intent, Uri uri ) {
    FileOutputStream fos = null;
    try {
        Bitmap bitmap = (Bitmap) intent.getExtras().get( "data" );
        File outputDir = activity.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" );
    } finally {
        try {
            if ( fos != null ) {
                fos.close();
            }
        } catch ( IOException e ) {
            Ln.e( e, "Error closing temp file for HTC Desire HD" );
        }
    }
    return uri;
}

如果您启动一个活动来拍照,那么在您的onActivityResult方法中,您会期望一个意图包含要下载的文件的 uri。如果没有,请使用以下代码:

        Uri uri = intent.getData();
        // HTC Desire Bug
        if ( uri == null && intent.getExtras() != null && intent.getExtras().get( "data" ) instanceof Bitmap ) {
            uri = createUriFromPhotoIntentForHtcDesireHD( activity, intent, uri );
            Ln.d( "The intent is %s", intent.getExtras().get( "data" ).getClass().getName() );
        }
于 2013-06-15T11:30:00.913 回答