2017-04-17 14 views
1

활동이 열리면 카메라에서 사진을 찍거나 갤러리에서 사진을 찍으라는 대화 상자 창이 열립니다. 단추를 눌러 카메라를 열면 응용 프로그램이 충돌하고 URI를 가져 오는 것과 관련된 Null Pointer 예외가 발생합니다. 나는 카메라에서 사진을 저장하기위한 Google의 안내를 따르고 있으며 문제가 어디에 있는지 찾을 수 없습니다. 오류가 발생파일의 URI를 가져올 때 널 포인터 예외가 발생합니다.

라인 :

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

항목 방법

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

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

매니페스트 :

<?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> 
0

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

메서드를 try-catch 절 안에 넣고 인쇄 예외를 사용하여 해당 URI에 무슨 일이 일어나는지 확인할 수 있습니다. – statosdotcom

+0

필자가 생각한 라인에서 확실히 발생합니다. 이것은 erorr'java.lang.NullPointerException : 가상 메소드 호출 시도 '입니다. android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData (android.content.pm. PackageManager, java.lang.String) 'null 객체 참조에' – Josh

+0

메소드가 실행하려고하는 액티비티의 CreatePostActivity 권한이 맞습니까? – statosdotcom

답변

0

경우 createImageFile 방법에 대한 코드 아래보십시오. 공급자가 존재하지 않기 때문에 예외가 발생합니다.

0

사람, 내가 대신 "fileprovider"의 "제공"을 입력 한 앞으로이 발견

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