2017-10-03 5 views
0

外部ストレージにファイルを保存したい場合は、Oreo androidバージョンで許可されていなければコードはokです。許可を得るにはこのコードを書きましたが、アンドロイドのプライマリストレージにアクセスするためのアクセス権を取得中にエラーが発生しました

int permission = ContextCompat.checkSelfPermission((Activity) ctx, 
      Manifest.permission.WRITE_EXTERNAL_STORAGE); 

    if (permission != PackageManager.PERMISSION_GRANTED) { 
     Log.i("per", "Permission to record denied"); 

     if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) ctx, 
       Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
      AlertDialog.Builder builder = new AlertDialog.Builder((Activity) ctx); 
      builder.setMessage("Permission to access the SD-CARD is required for this app to Download PDF.") 
        .setTitle("Permission required"); 

      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int id) { 
        Log.i("click", "Clicked"); 
        makeRequest(); 
       } 
      }); 

      AlertDialog dialog = builder.create(); 
      dialog.show(); 

     } else { 
      makeRequest(); 
     } 
    } 
    protected void makeRequest() { 
     ActivityCompat.requestPermissions((Activity) ctx, 
     new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
     REQUEST_WRITE_STORAGE); 
     file= new File(Environment.getExternalStorageDirectory(), 
     downloadFilePathHash); 

} 

私が取得エラー: enter image description here

+0

に応答した後でしたあなたはあなたのスタックトレースを共有しますか? – slesh

答えて

1

だけの提案、RxPermissionsに見て。権限管理全体を1つのビルダーパターンに置き換えます。あなたはbuild.gradle

で述べたRxJava依存性はまた、あなたの問題に来て持って、あなたはこの部分を行う必要がありますことを確認してください:

onActivityResult

file= new File(Environment.getExternalStorageDirectory()

ユーザーがあなたの要求

関連する問題