2017-12-27 157 views
0

由于文档描述在这里Documentation我试图通过意向访问全尺寸图像保存。据我了解,我已按照文档中的每一步操作。现在我想知道如何访问该文件onActivityResult()。因为该方法上的Intent为空。如何获得在Uri后的全尺寸照片保存后

Activity.java

private void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     // Ensure that there's a camera activity to handle the intent 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      // Create the File where the photo should go 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
      } catch (IOException ex) { 
       // Error occurred while creating the File 
      } 
      // Continue only if the File was successfully created 
      if (photoFile != null) { 
       Uri photoURI = FileProvider.getUriForFile(this, 
         "com.example.chathurangashan.cameraintent", 
         photoFile); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
       startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
      } 
     } 

    } 

    private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
     File image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       storageDir  /* directory */ 
     ); 

     // Save a file: path for use with ACTION_VIEW intents 
     mCurrentPhotoPath = image.getAbsolutePath(); 
     return image; 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_IMAGE_CAPTURE) { 
      if (resultCode == RESULT_OK) { 
       if (data.hasExtra(MediaStore.EXTRA_OUTPUT)) { 
        Uri uri = (Uri) data.getParcelableExtra(MediaStore.EXTRA_OUTPUT); 
       } 
      } 
     } 
    } 

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.chathurangashan.cameraintent"> 

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

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


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="com.example.chathurangashan.cameraintent" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/file_paths"/> 
     </provider> 


     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

file_path.xml

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path 
     name="my_images" 
     path="Android/data/com.example.chathurangashan.cameraintent/files/Pictures" /> 
</paths> 

file_path.xml是资源文件夹下保存在 '资源'

PS:我知道我能得到的位图中onActivityResult()直接作为文档下,描述“获取缩略图”但它没有返回原图。它是拍摄图像的低质量版本。

+0

“它返回null intent onActivityResult” - 您可能想编辑您的问题并发布整个Java堆栈跟踪。请注意,'EXTRA_OUTPUT'是您在'ACTION_IMAGE_CAPTURE''Intent'上提供的值。它不是返回数据的一部分。 – CommonsWare

+0

谢谢。编辑。 –

回答

0

dispatchTakePictureIntent()应返回的意图,这样你可以得到乌里路径作为额外的,回用MediaStore.EXTRA_OUTPUT关键。

private Intent dispatchTakePictureIntent() { 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      // Ensure that there's a camera activity to handle the intent 
      if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
       // Create the File where the photo should go 
       File photoFile = null; 
       try { 
        photoFile = createImageFile(); 
       } catch (IOException ex) { 
        // Error occurred while creating the File 
       } 
       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        Uri photoURI = FileProvider.getUriForFile(this, 
          "com.example.chathurangashan.cameraintent", 
          photoFile); 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
       } 
      } 
      return takePictureIntent; 
     } 

现在你应该开始你的意图,像这样和全球存储在某个地方供以后使用。

intentImage = dispatchTakePictureIntent(); 
startActivityForResult(intentImage, REQUEST_IMAGE_CAPTURE); 

现在在你的onActivityResult做这样的事情。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_IMAGE_CAPTURE) { 
     if (resultCode == RESULT_OK) { 
      if (intentImage.hasExtra(MediaStore.EXTRA_OUTPUT)) { 
       //your uri from saved intent 
       Uri uri = Uri.parse(intentImage.getStringExtra(MediaStore.EXTRA_OUTPUT)); 
      } 
     } 
    } 
} 
+0

Is'n那个函数返回Intent和调用startActivityForResult之外的东西是一样的吗?我以任何方式尝试相同的结果。并且'return Intent'应该是'return takePictureIntent',对吧? –

+0

是的,但我希望你有想法。我们把uri放在意图中,当结果出来时让我们从相同的意图中提取出来。 –