Issue
I want to make a pubg gfx tool for , but I can't access pubg directory Com.pubg.krmobile
in Android/data
Full Path: /Android/data/Com.pubg.krmobile
Tried Nothing (Because I am new I don't know much coding.
Q: How to access this specfic folder Com.pubg.Krmobile
of android/data
like this app:
Solution
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
Answered By - Yahya Durrani
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.