1

我正在尝试获取使用相机意图作为字符串拍摄的图片的文件路径,但字符串 filePath 始终为空。我究竟做错了什么?

       public void onClick(View arg0) {
        switch (arg0.getId()) {     
         case R.id.btnImageCapture:
            Intent openCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
               startActivityForResult(openCamera, OPEN_CAMERA); 
                 break;
           }
        }



        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         switch(requestCode){
         case OPEN_CAMERA:
             if (resultCode == RESULT_OK && data != null) {
                 Uri captureImage = data.getData();
                 String filePath = captureImage.getPath();
                 break;
              } 
           }
        }
4

2 回答 2

1

这就是我如何获得用相机拍摄的图像。

我在保存图像之前创建文件,然后将其保存到我的文件中。

File externalFile = new File("Whatever you want the path to be...");
Uri uriSavedImage=Uri.fromFile(externalFile);
Intent launchcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
launchcameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(launchcameraIntent,CAMERA_PIC_REQUEST);

然后在收到结果时。

protected void onActivityResult(int requestCode,int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAMERA_PIC_REQUEST) {
                  Bitmap photo = BitmapUtils.decodeFileForDisplay(new File("Whatever your file's path is");
            }
        }
}
于 2013-02-22T14:29:06.633 回答
1

尝试以下将您的captureImage Uri 作为参数传递:

    public String getRealPathFromURI(Uri contentUri) {
        String[] projx = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, projx, null, null, null);
         int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

编辑:

这是 getData() 在某些设备上返回 null 的常见错误。您需要使用预先插入的 Uri 来防止这种情况。例子:

  Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
 preinsertedUri = getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new ContentValues());

        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

得到结果:

         @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {

    case CAMERA_PIC_REQUEST:

        if (resultCode != 0 && data != null) {

            Uri imageUri = preinsertedUri;
            }
              break; 
                 }
于 2013-02-22T14:45:36.370 回答