2017-12-27 3 views
0

Als Dokumentation hier beschreiben Documentation Ich versuche, Zugriff auf Full-Size-Bild durch Absicht zu erhalten. Ich habe jeden Schritt in der Dokumentation nachvollzogen, wie ich es noch verstehe. Jetzt frage ich mich, wie man auf diese Datei zugreifen kann onActivityResult(). weil Intent für diese Methode null ist.So erhalten Sie Zugriff auf das Originalfoto nach Uri nach dem Speichern

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 ist unter Ressourcenordner in 'res' speichern

PS: Ich weiß, ich kann die Bitmap in onActivityResult() direkt als Dokumentation unter beschreiben bekommen „Hier die Vorschau“ aber es ist nicht Bild in voller Größe zurück. es ist eine minderwertige Version des aufgenommenen Bildes. (

+0

"Es gibt Null Absicht onActivityResult" - Sie möchten vielleicht Ihre Frage bearbeiten und den gesamten Java-Stack-Trace veröffentlichen. Beachten Sie, dass 'EXTRA_OUTPUT' ein Wert ist, den Sie in' ACTION_IMAGE_CAPTURE' 'Intent' angeben. Es ist nicht Teil der zurückgegebenen Daten. – CommonsWare

+0

Danke. Bearbeitet. –

Antwort

0

Ihre dispatchTakePictureIntent) sollte eine Absicht zurückzukehren, so dass Sie Uri Weg als Extra zu bekommen, mit MediaStore.EXTRA_OUTPUT als Schlüssel zurück.

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

Jetzt sollten Sie Ihre Absicht wie folgt starten und irgendwo speichern, um sie später zu verwenden.

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

jetzt in Ihrem onActivityResult tun Sie so etwas.

@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

Ist das das Gleiche mit der Funktion Intent zurückgeben und startActivityForResult außerhalb aufrufen? Irgendwie habe ich das gleiche Ergebnis versucht. und das 'return Intent' sollte' takePictureIntent 'sein, oder? –

+0

ja, aber ich hoffe, du hast die Idee. Wir setzen das URI in die Absicht und wenn Ergebnis kam, lasst es aus der gleichen Absicht herauskommen. –

Verwandte Themen