How to rename the file on android R and above - java

I have an app in which i want to change the name of audio files in music directory. On Api level < 28 i use to do this.
File file = new File(absolutepath);
file.renameTo("new name");
I have tried to look up many resources for the possibility of doing this.
Note: Above provided code works for files generated by my app (On API 30) but if I unistall the app and install it again then i loose the permission to rename the file.
Also I dont want to use MANAGE_EXTERNAL_STORAGE permisssion.

Related

Unable to get files from folder with .nomedia file or hidden folder in Android 11

I want to get all files from WhatsApp's .Statuses folder. Until Android 10 im perfectly getting all statuses files. But on Android 11 due to new restrictions, when I code like below:
File(Environment.getExternalStorageDirectory().absolutePath + File.separator + "Android/media/com.whatsapp/WhatsApp/Media/.Statuses").listFiles()
I always get 0 files. Whereas, Im successfully getting other folder files "Android/media/com.whatsapp/WhatsApp/Media/" on this path.
Two problems I'm facing now:
If a folder is hidden then in Android 11, listFiles() returns 0 on that folder.
If a folder not hidden but contains one file as ".nomedia" , listFiles() returns 0 on that folder as well in Android 11.
What should I do to get all whatsapp statuses files in Android 11?
I dont want to use MANAGE_EXTERNAL_STORAGE permission for it due to google policies. Thank you
There are ways to access Hidden Directory in Android 11, two of them are:
Storage access framework (SAF) in which you take user to that specific directory and ask for permission from user to access directory files and that way you get access to files in it (Study SAF).
You can use File Observer on that hidden folder and whenever any file created or modified or deleted from that hidden directory you will get full path in that case for that specific file in hidden folder & once you have full path you can have access to that file.

How can I find the main directory on my Android app?

I would like my Android app to find the main directory on my tablet's internal storage. The furthest I can get is reading the contents of the "user.dir", but that seems to be the root directory and the listing there is too immense to find what would be considered the DOS equivalent of C:\
I am listing the code snippet I use to read the directory to show I can access the internal storage, but somewhere I'm not able to do it correctly.
workingDir = System.getProperty("user.dir");
File f = new File(workingDir);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
Thanks
in fact, if you were to open the "My Files" explorer that comes with Android the top option on my tablet is called Internal Storage, and it lists all the viewable folders from within
That is what the Android SDK refers to as external storage. You can find the root of that via Environment.getExternalStorageDirectory(). However:
Don't put things directly there, just as well-written Windows programs haven't put stuff in C: for the past couple of decades. Use Environment.getExternalStoragePublicDirectory() for standard locations (e.g., downloads directory), or use getExternalFilesDir() on Context for a location that is unique to your app.
You need to hold READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE permissions, including setting up runtime permissions for use on Android 6.0+ devices.
You can try to give a look at the android Environment documentation and access internal storage root/main directory with this Environment.getRootDirectory()

Getting name of original .APK file in Android

I am trying to figure out how to get the name of the original .apk file from which an application was installed. I know that you can retrieve the package name and the source directory of the installed application by using the ApplicationInfo class (described here: Android: how to get the name of apk file programmatically?) but this is NOT what I want.
As an example, suppose I have a simple application that is named TestApp (via the android:label tag in the AndroidManifest.xml file). Furthermore, suppose I generate an .apk file for the application above, rename said .apk file to TestApp_JohnDoe.apk, and then email that application to John Doe for installation. When John Doe installs the application by clicking on the .apk file I would like to be able to read the filename of the original .apk, TestApp_JohnDoe.apk, that I sent to him. Using the ApplicationInfo class, more specifically the sourceDir method gives me the install directory and application name, /data/app/TestApp.apk, which is not what I am looking for. I know that it is probably possible to scan all available directories looking for the original .apk file, but I am hoping to avoid this.
In summary, I would like to programatically retrieve the .apk filename/source directory of the installer .apk, such as /storage/sdcard0/Download/TestApp_JohnDoe.apk, as opposed to the .apk filename/source directory of the actual installed application, /data/app/TestApp.apk.
It is possible to let the app know the apk name that was set at compile time, if that is of any use to you
applicationVariants.all { variant ->
// Change the target apk file name and path
String apk_path = "$rootProject.projectDir/bin"
String apk_name = "john_doe.apk"
project.delete(project.apk_path) //Delete any remains
String apkFile = String.format("%s/%s", apk_path, apk_name)
variant.outputs[0].outputFile = new File(apkFile)
// This can be used in Java runtime by using getString(R.string.apk_name)
resValue "string", "apk_name", apk_name
}
But what you are asking, I am pretty sure it is not possible

Android - store and load files in project root

I am looking for an appropriate way to store and access pdf files in my Android app.
The problem is: I have 5 pdf-files which I would like to store in the project resources and load in the app.
I have tried to make a new directory in the project root /myApp/pdfs, and accessing it by:
File myFile = new File("/myApp/pdfs/,fileName);
I have tried some different variants of the path name: ./myApp/pdfs, myApp/pdfs, /pdfs, ./pdfs. However I get the same message stating that the file can't be found.
How do I get the path to my apps directory? And is this the most appropriate approach for saving a small number of pdf-files?
If you are wanting to load the pdfs from the app (while the app is running), you probably want to store them in the res/raw folder.

How to access class file in android

I want to acess class file from bin folder in android.
I was doing it using
File f = new File("/bin/filename.class");
Its working fine in java but in android giving path doesnt work, so
please suggest me other way to access class file of any java file in android.
You can't access file like
File f = new File("/bin/filename.class");
from bin folder. Android can't recognize this path.
Java .class files are converted to Dalvik Executable (.dex), in your apk there won't be any .class files, just one single dex file called classes.dex.
I think this post from Android Developers blog might help you: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html

Categories

Resources