Search code examples
androidfilepermissions

Android: I can open a file by my app ONLY if I have created (If I copy outside the app, it gets ACCESS DENIED)


[![Folder whith files (beginning)][1]][1]

Folder with files (end)

In the second image...

  • I can open the last file (JSON Web3j credentials).... But I cannot open the before last file (in example)...
  • The first (last file) was created by the app... The other was copied there using the Android file manager you see in the pictures.
  • So... two files existing, and in the same directory... One is openend by my app, And the other does not!!!

I guess it is a problem of permits,... or something like that. But I do not know how to open a file not created by my app.

I have permits managed in AndroidManifest.xml and with:

    if (ActivityCompat.checkSelfPermission(this
      , Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
          new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE
          , Manifest.permission.READ_EXTERNAL_STORAGE},
          k_id_requestpermissions_external_storage);
    }

Can yo help me? Thanks a lot...


Solution

  • Finally I found the problem and the solution...

    UNFORTUNATELY Google Play Console will present a lot of rejections to allow your app to have that permission... (problem->solution->new problem...)

    I needed an extra permit:

    • AndroidManifest.xml
        <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    
    • Main Activity... in OnStart (i.e.)
    intent_activityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult()
        , new ActivityResultCallback<ActivityResult>() {
          @Override
          public void onActivityResult(ActivityResult result) {
              ResourceBundle in;
              try {
                  in = ResourceBundles.getBundle(k_in_ruta);
                  if (result.getResultCode() != RESULT_CANCELED) {
                      put_alert("No permit! ");
                  }
              } catch (Exception e) {
                  try {
                      put_alert(e);
                  } catch (Exception e_ignorar) {}
              }
          }
    });
    if (android.os.Build.VERSION.SDK_INT >= 30) {
        if (Environment.isExternalStorageManager() == false) {
            Intent intent = new Intent();
             intent.setAction (Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
             Uri uri = Uri.fromParts("package", this.getPackageName(), null);
             intent.setData(uri);
             intent_activityResultLauncher.launch(intent);
        }
    }