2013-07-02 82 views
3

我这样做:告诉相机意图保存拍摄的图像

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) 

如果我检查了画廊,照片是不存在的,所以我的猜测是,其意图是无视MediaStore.EXTRA_OUTPUT值。

有什么我可以做的,不涉及编写我自己的相机解决方案?

+2

你有没有把这个权限放在menifest

+0

真的需要吗?我的意思是,如果我在没有互联网许可的情况下使用共享意图,我仍然可以在Twitter上分享。 – razielsarafan

回答

3

使用这样:

意图:

   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; 
    } 
    } 

的manifest.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" /> 

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

+0

什么是“selectedImage”? – razielsarafan

+0

接收图像的结果。 – Nirmal

+1

Nvm,没有看到它。 'imageReturnedIntent.getData();'无论如何都返回null。 – razielsarafan

1

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

相关问题