Advertisement
Guest User

Untitled

a guest
Aug 6th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1.  
  2.     public void writeByteArraysToFile(String fileName, byte[] content) {
  3.         try {
  4.             File sciezka = this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
  5.             File file=new File(sciezka,fileName);
  6.             file.setReadable(true);
  7.             file.setExecutable(true);
  8.             FileOutputStream fos = new FileOutputStream(file);
  9.             fos.write(content);
  10.             fos.close();
  11.         }
  12.         catch (Exception e)
  13.         {
  14.             Tools.toastMessage(getApplicationContext(), "Wystąpił problem: " + e.getMessage());
  15.         }
  16.     }
  17.  
  18.     public void getPermission(){
  19.         if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
  20.             if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE))
  21.             {
  22.  
  23.             }
  24.             else
  25.             {
  26.                 ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
  27.             }
  28.         }
  29.         else
  30.         {
  31.             if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
  32.                 if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE))
  33.                 {
  34.  
  35.                 }
  36.                 else
  37.                 {
  38.                     ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
  39.                 }
  40.             }
  41.         }
  42.     }
  43.  
  44.  
  45.     @Override
  46.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  47.         switch (requestCode){
  48.         case -1:
  49.             //hello
  50.             break;
  51.         case MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE:
  52.             if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
  53.             {
  54.                 getPermission();
  55.             }
  56.             else
  57.             {
  58.                 Toast.makeText(this,"You've denied permission",Toast.LENGTH_LONG).show();
  59.             }
  60.             return;
  61.         case MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE:
  62.             if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
  63.             {
  64.                 getPermission();
  65.             }
  66.             else
  67.             {
  68.                 Toast.makeText(this,"You've denied permission",Toast.LENGTH_LONG).show();
  69.             }
  70.             return;
  71.         default:
  72.             super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  73.         }
  74.  
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement