Getting name of original .APK file in Android - java

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

Related

How to rename the file on android R and above

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.

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.

Find a directory in Android Studio Project not in the phone

I am trying to get access to a text file (log.txt) in a directory. All other questions on this topic refer to getting directories from the emulators internal storage.
My file structure is as such
>androidApp
->App
-->Build
-->src
--->game_log
---->log.txt
--->Main
---->(Android app Code further)
Using new File(System.getProperty("user.dir) + "app\\src\\game_log\\log.txt").exists() gets me false.
Another thing I tried was System.getProperty("user.dir") but that yields me /.
Contextwrapper.getPath() gets me the path of the emulators storage.
Is the file structure of Android Studio different or I am using the wrong method to get the file from my project folder?

In netbeans, how to save or access an image after deploying jar file

I am trying to make a mess management application in Java using NetBeans. I want to save images of Members in a specified folder inside my src directory. I just created folder named EmpImgs for storing employees images. Here is my code:
File srcDir = new File(file); // current path of image
File dstDir = new File("src\\J_Mess_Mgnt\\EmpImgs\\"+Txt_C_G_M_M_ID.getText());
objm.copyFile(srcDir, dstDir);` // copy image from srcDir to dstDir
Here I use another class for copying images to predefined folders and renaming the images based on their ID.
Everything is working properly in Java IDE.
But unfortunately after making an executable .jar file, this code will not work. I cannot save or access any image file in that directory.
I just went through this site, but I didn't find a suitable answer.
All I need is saving and editing images inside jar folder
Hehe hi mate you need some help. This is a duplicate but I will cut you some slack and maybe you should delete this later. So back to basics, the jvm runs byte code, which you get from compiling java source code to .class files. Now this is different to C and C++ were you just get a .exe. You don't want to give your users a bunch of .class files in all these folders which they can edit and must run a command on the command line, but instead give them what is known as an 'archive' which is just an imutable file structure so they can't screw up the application, known as a jar in java. They can just double click on the archive (which is a jar), and the jvm will call the main method specified in the MetaInf directory (just some information about the jar, same as a manifest in other programming languages).
Now remember your application is now a jar! It is immutable! for the resasons I explained. You can't save anymore data there! Your program will still work on the command line and in IDEs because it is working as if you used your application is distrubuted as bunch of folders with the .class files, and you can write to this location.
If you want to package resources with your application you need to use streams (google it). BUT REMEMBER! you cant then save more resources into the jar! You need to write somewhere else! Maybe use a user.home directory! or a location specified from the class path and the photos will be right next to the jar! Sometimes you might need an installer for your java application, but usually you don't want to create the extra work if you don't need to.
At last I find an answer suit for my question.It is not possible to copy images or files to a executive jar folder.So I used a different Idea.Create some folders(as per our requirement),Where my executable jar folder is located(No matter which drive or where the location is).The code is..
String PRJT_PATH=""; //variable to store path of working directory.
private void getdire() throws IOException{
File f=new File(".");
File[] f1=f.listFiles();
PRJT_PATH=f.getCanonicalPath(); //get path details.for eg:-E:/java/dist
}
private void new_Doc_folder(){ //function for creating new folders
try{
String strManyDirectories="Docs"+File.separator+"Bil_Img"; //i need to create 2 folders,1st a folder namedDocs and In Docs folder another folder named Bil_Img
String SubDirectories="Docs"+File.separator+"EmpImgs"; //same as above but keep in mind that It will not create a Same folder again if already exists,
// Create one directory
boolean success = (new File(strManyDirectories)).mkdirs(); //create more than one directory
boolean success1 = (new File(SubDirectories)).mkdir(); //Creates a single directory
if (success && success1) {
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
It works Successfully.
Regds

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.

Categories

Resources