skip to Main Content

I want to make a tool for , but I can’t access its directory Com.tool.mobile in Android/data

Full Path: /Android/data/Com.tool.mobile

Tried Nothing (Because I am new I don’t know much coding.

Q: How to access this specfic folder Com.tool.mobile of android/data

like this app:

  1. Screenshot: Error – Special Permission is required

  2. Screenshot: Dialog – Allow Tool to access files in data

  3. App Link

2

Answers


  1. From Android 10 with Scoped Storage if you are targeting to SDK 30 you can’t access to others app’s files even if they are in external storage, at least until you request and got the approval from Google to do so, specifically you will need All Files Access Permission.

    To know more see this video resource in YouTube from the official Android Developers channel.

    You can temporary opt-out this restriction by setting your target SDK to 29 & using the requestLegacyExternalStorage flag to true in your app manifest, but you will need it to publish your app or app updates in the next November from this link about Google Play’s target API level requirement:

    Starting in November 2021, app updates will be required to target API
    level 30 or above and adjust for behavioral changes in Android 11.
    Existing apps that are not receiving updates are unaffected and can
    continue to be downloaded from the Play Store. Wear OS apps must
    continue to target API level 28 or higher.

    Note: Before doing the request to get the approval from Google to get All Files Access Permission, see if your app really requires this permission, usually it was designed for apps that do files management or backups, and if your justifications is not good enough your request might be rejected.

    Login or Signup to reply.
  2. okay so all of these people saying you cannot access app specific folders, are absolutely wrong, heres how u do it:

    Step 1: get the permission of the folder you want,

    public void openDirectory() {
    
    String path = Environment.getExternalStorageDirectory() + "/Android/data/com.pubg.krmobile/whatever folder you want to access";
    File file = new File(path);
    String startDir, secondDir, finalDirPath;
    
    if (file.exists()) {
        startDir = "Android%2Fdata%2Fcom.pubg.krmobile%2Fthefilder%2Fsubfolder%2Fdeepersubfolder";
    } 
    
    StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
    
    Intent intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
    
    
    Uri uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI");
    
    String scheme = uri.toString();
    
    Log.d("TAG", "INITIAL_URI scheme: " + scheme);
    
    scheme = scheme.replace("/root/", "/document/");
    
    finalDirPath = scheme + "%3A" + startDir;
    
    uri = Uri.parse(finalDirPath);
    
    intent.putExtra("android.provider.extra.INITIAL_URI", uri);
    
    Log.d("TAG", "uri: " + uri.toString());
    
    try {
        startActivityForResult(intent, 6);
    } catch (ActivityNotFoundException ignored) {
    
    }}
    

    step 2: write an onactivityresult to get the path

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        if (resultCode == RESULT_OK) {
            if (data != null) {
                uri = data.getData();
                if (uri.getPath().endsWith(".Statuses")) {
                    Log.d("TAG", "onActivityResult: " + uri.getPath());
                    final int takeFlags = data.getFlags()
                                                  & (Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        getContentResolver().takePersistableUriPermission(uri, takeFlags);
                    }
                    
                    // these are my SharedPerfernce values for remembering the path
                    prefHelper.setIsScopePermissionGranted(true);
                    prefHelper.setSavedRoute(uri.toString());
                    
                    // save any boolean in pref if user given the right path so we can use the path 
                    // in future and avoid to ask permission more than one time
                    
                    startActivity(new Intent(this, MainDashboardActivity.class));
                    finish();
                    
                } else {
                    // dialog when user gave wrong path
                    showWrongPathDialog();
                }
                
            }
        }}
    

    step 3: Get the files one by one from here

     if (Build.VERSION.SDK_INT >= 29) {
                // uri is the path which we've saved in our shared pref
                DocumentFile fromTreeUri = DocumentFile.fromTreeUri(context, uri);
                DocumentFile[] documentFiles = fromTreeUri.listFiles();
                
                
                for (int i = 0; i < documentFiles.length; i++) {
                    documentFiles[i].getUri().toString() //uri of the document
                    }
                }
            }
    

    and voila, you now have access to that folder using the path saved in shared prefrences

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search