2017-04-17 2 views
1

Lorsque l'activité s'ouvre, une boîte de dialogue s'ouvre demandant à l'utilisateur de prendre une photo de la caméra ou d'en choisir une dans la galerie. Dès que j'appuie sur le bouton pour ouvrir la caméra, l'application se bloque et j'obtiens une exception Null Pointer liée à l'obtention de l'URI. J'ai suivi la procédure pas à pas de Google pour enregistrer des images à partir de la caméra et n'arrive pas à trouver l'origine du problème.Obtention d'une exception de pointeur nul lors de la tentative d'obtention de l'URI d'un fichier

ligne où l'erreur se produit:

Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this, 
           "xyz.beerme.beerme.provider", 
           photoFile); 

procédé total:

cameraButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if(intent.resolveActivity(getPackageManager()) != null){ 
        File photoFile = null; 
        try{ 
         photoFile = createImageFile(); 
        } catch (IOException ex){ 
         Snackbar message = Snackbar.make(findViewById(R.id.activity_create_post), "Error creating image", Snackbar.LENGTH_LONG); 
         message.show(); 
        } 
        if(photoFile != null){ 
         Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this, 
           "xyz.beerme.beerme.provider", 
           photoFile); 
         intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
         startActivityForResult(intent, REQUEST_CAMERA); 
        } 
       } 
       dialog.dismiss(); 
      } 
     }); 

méthode createImageFile:

private File createImageFile() throws IOException{ 
     String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_beerme"; 
     File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
     File image = File.createTempFile(
       imageFileName, 
       ".jpg", 
       storageDir 
     ); 

     mCurrentPhotoPath = image.getAbsolutePath(); 
     return image; 
    } 

manifeste:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="xyz.beerme.beerme"> 

    <!-- To auto-complete the email text field in the login form with the user's emails --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.READ_PROFILE" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     tools:replace="android:supportsRtl"> 
     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="redacted" /> 
     <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="xyz.beerme.beerme.fileprovider" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/file_paths" ></meta-data> 
     </provider> 
     <activity android:name=".PostsActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.categroy.LAUNCHER"/> 
      </intent-filter> 
     </activity> 
     <activity android:name=".CreatePostActivity"></activity> 
    </application> 

</manifest> 

file_paths.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/xyz.beerme.beerme/files/Pictures" /> 
</paths> 
+0

Vous pouvez joindre votre méthode dans une clause et d'impression exception try-catch pour voir ce qui se passe à cette URI. – statosdotcom

+0

il est certainement se produire à la ligne que je pensais, c'est le erorr 'java.lang.NullPointerException: Tentative d'invoquer la méthode virtuelle « android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData (de android.content.pm. PackageManager, java.lang.String) 'sur une référence d'objet null' – Josh

+0

L'activité où votre méthode essaie de s'exécuter a le nom de CreatePostActivity non? – statosdotcom

Répondre

0

Dans le cas où quelqu'un trouve cela à l'avenir, je tapais "fournisseur" au lieu de "fileprovider". Comme le fournisseur n'existe pas, c'est la source de l'exception.

0

Essayez le code ci-dessous pour la méthode createImageFile

private File createImageFile() { 
    File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    // path = path + (timeStamp + "1jpg"); 

    try { 
     file = File.createTempFile(timeStamp, ".jpg", storageDir); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (Build.VERSION.SDK_INT >= 24) 
     mCurrentPhotoPath = String.valueOf(FileProvider.getUriForFile(MainActivity.this, 
       BuildConfig.APPLICATION_ID + ".provider", file)); 
    else 
    mCurrentPhotoPath = String.valueOf(Uri.fromFile(file)); 
    return file; 
}