【问题标题】:Tell the Camera Intent to save the taken image告诉相机意图保存拍摄的图像
【发布时间】:2013-06-29 20:31:56
【问题描述】:

我正在这样做:

intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final File storage = Environment.getExternalStorageDirectory();
final Uri uri = Uri.fromFile(new File(storage, System.currentTimeMillis() + ".jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, id);

为了得到那张照片,我这样做:

private String getLastImagePath() {
    final String[] imageColumns = { MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    final Cursor imageCursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
            null, null, imageOrderBy);
    if (imageCursor.moveToFirst()) {
        final String fullPath = imageCursor.getString(imageCursor
                .getColumnIndex(MediaStore.Images.Media.DATA));
        return fullPath;
    } else {
        throw new RuntimeException();
    }
}

但是,我不断收到这样的消息:

07-02 14:46:54.751: E/BitmapFactory(23119): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20130702_144653.jpg: open failed: ENOENT (No such file or directory)

如果我检查图库,照片不在那里,所以我的猜测是 Intent 忽略了 MediaStore.EXTRA_OUTPUT 值。

除了编写自己的相机解决方案之外,我还能做什么?

【问题讨论】:

  • 你有没有把这个权限放在清单中
  • 真的需要吗?我的意思是,如果我对没有 Internet 权限的应用使用 Share Intent,我仍然可以在 Twitter 上分享它。

标签: android android-intent android-camera mediastore


【解决方案1】:

这样使用:

意图:

             Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePicture, 0);

获取该结果:

 protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

       switch(requestCode) {
       case 0:
           if(resultCode == RESULT_OK){
           Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                   Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                   cursor.moveToFirst();

                   int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                  //file path of captured image
                   filePath = cursor.getString(columnIndex); 
                   //file path of captured image
                   File f = new File(filePath);
                   filename= f.getName();

                   Toast.makeText(SiteViewFieldCreate.this, "Your Path:"+filePath, 2000).show();
                   Toast.makeText(SiteViewFieldCreate.this, "Your Filename:"+filename, 2000).show();
                   cursor.close();

                 //Convert file path into bitmap image using below line.
                  // yourSelectedImage = BitmapFactory.decodeFile(filePath);


                  //put bitmapimage in your imageview
                  //yourimgView.setImageBitmap(yourSelectedImage);
      }
       break;
     }
    } 

将此添加到您的 ma​​nifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />


<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

希望这会给你一些解决方案。

【讨论】:

  • 接收图像的结果。
  • Nvm,没看到。 imageReturnedIntent.getData(); 无论如何都会返回 null。
  • 遵循该代码。因为它对我有用。请也参考此stackoverflow.com/a/2737770/1835764 可能会对您有所帮助。
  • 签入设备。不要签入模拟器。
  • 我正在检查 Nexus 4。
【解决方案2】:

Nirmals 解决方案适用于除 Android 4.x(Apilevel 15)以外的所有设备。 似乎是一个错误,但imageReturnedIntent.getData() 总是返回 null! (模拟器和设备上的testet) 拍摄的照片甚至没有保存在 externaldir 中,因此任何从ContentResolver 获取最新的id 或最大的date_added 的尝试都将失败(返回卡中的最新照片,但不返回拍摄的照片)! 目前我没有解决这个问题...似乎不可能在 Apilevel 15 上获得拍摄照片的 路径 (虽然仍然可以获取可绘制并自己保存).. .

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多