How to find out the AD Junk present in the device? - java

I am trying to find out the Ad Junk present (Files which are Downloaded by the AD SDK of other apps).
This is what I have been able to find out till now -
I am loading the list of all the files in the devices and them checking them and grouping them like this
if (file.getPath().toLowerCase().endsWith(".temp") )
{
//Temp Files found
}
else if (file.getName().endsWith(".apk") && file.canWrite())
{
//Apk Files found
}
There are many other cleaner which find out the junk files present in the Device. Even I am trying to achieve the same. Can any one help me on how to identify whether the file is an AD Junk file or not?

You can Delete Junk File by
File[] files = getBaseContext().getCacheDir().listFiles();
for (File file : files) {
file.delete();
}
And Put this permissions in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

You could check the codebase of some Open Source Cleaner, like: https://github.com/mzlogin/CleanExpert
or
https://github.com/DroidsOnRoids/android-device-cleaner
and see how they do it.

Related

Not able to Use RandomAccessFile file for rw mode only in case when writing a file in sd card

Below is my sdcard path of a start.mp4 file. I am able to get this file in read mode but not able to open in rw mode. I have given runtime permissions also. It throws a exception:
/storage/3263-6232/piyush/Download/start.mp4: open failed: EACCES (Permission denied)
Code:
String sdCardPath = "/storage/3263-6232/piyush/Download/start.mp4";
File file = new File(sdCardPath );
try{
RandomAccessFile rfs = new RandomAccessFile(file, "rw");
rfs.seek(file.length());
rfs.close();
} catch (IOException e) {
e.printStackTrace();
}
In the above code I have taken sdcardpath to a file which exists in sdcard.
Than after that whenever I tried to open that file in outputstream using RandomAccessFile it gives FilenotFound Exception:
/storage/3263-6232/piyush/Download/start.mp4: open failed: EACCES (Permission denied)
Does your Manifest looks like this?
The uses-permission tag needs to be outside the application tag
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
<application>
...
<activity>
...
</activity>
</application>
</manifest>
PS: If you are using Post M device for testing then make sure you ask WRITE_EXTERNAL_STORAGE permission before doing any operation on you file.
Change your Storage location to /storage/3263-6232/Android/data/<Your package Name>/files will solve your issue
Above Path is only have write access for related third party apps, elsewhere you can read the file but can't write new file.
Or you need to apply Storage Access Framework for getting write permission.
For more details you can visit
https://developer.android.com/guide/topics/providers/document-provider.html

Create folder in the ExternalStorage

I am trying to write something to the ExternalStorageDirectory on my Android device. To test this, I tried to create a simple folder in this directory. As it has not been working I stumbled across this stackoverflow thread. When I use the most upvoted example code
File folder = new File(Environment.getExternalStorageDirectory() + "/map");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
// Do something on success
} else {
// Do something else on failure
}
and set the givven WRITE_EXTERNAL_STORAGE permission, the creation of the folder is always failing - there is no exception, but after the execution f.exists() is still false.
I tested this on my Nexus 5X and the latest Nexus 5X-Emulator.
In the emulator Environment.getExternalStorageState() returns not-mounted although I mounted the virtual SD-card (this seems also strange to me). In my physical device it returns mounted - so I do not see any problem, why the code should not be working.
Any advices for the solution of the failure of the creation of the folder and/or the behaviour of the Environment.getExternalStorageState() are welcome.
Make sure to add this permission into manifiest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and create folder like this
String folder_main = "map";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}

get removable sd card path in android

How can i get extSdcard path in android?
There are 2 storage, first external storage in which all the phones have it but there is second storage which is called removable storage (micro sdcard).
I want to get the path to the micro sdcard in android, how is that possible?
Starting from KitKat, you have access to a method to get that directory :
Context.getExternalFilesDirs()
Note that it may return null if the SD card is not mounted or moved during the access. Also, you might need to add these permissions to your manifest file :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Apparently some users had trouble with this method, not returning the SD card path. I found some useful informations on how it can works and how it can not depending on the device on this post.
Check this https://developer.android.com/guide/topics/data/data-storage.html#filesExternal for complete reference as you need to define permission to read and write to the external SD also there is code in the above link which checks if SD card is available before performing any action
The following works for me!
File[] Dirs = ContextCompat.getExternalFilesDirs(this.getApplicationContext(), null);
File path = (Dirs[1]);
String pathSD = Dirs[1].toString();
int firstOpen = pathSD.indexOf("/");
int secondOpen = pathSD.indexOf("/", firstOpen + 1);
int thirdOpen = pathSD.indexOf("/", secondOpen + 1);
String filename = pathSD.substring(firstOpen, thirdOpen + 1);
path = new File(filename);
As of API level 24, you can use the StorageManager class:
https://developer.android.com/reference/android/os/storage/StorageManager#getStorageVolumes()

Can't read files from SDcard folder in Android device?

I am trying to access WhatsApp database folder exists in Environment.getExternalStorageDirectory().getAbsolutePath() + /WhatsApp/Databases location in un-rooted device.
My aim is to collect these files and decrypt them and show each messages to Web portal. Here is the code to collect these files:
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + location;
f = new File(path);
final File file[]=f.listFiles();
final int size = file.length;
Whenever i am running on rooted device, It is working but on some unrooted devices it is throwing nullpointer because it is not able to get the list of all database files.
My question is: Is it possible to ready WhatsApp Database files when device is not root?
or is there any way to do it?
I am not sure the root effects. Have you get the PERMISSION for your app ? if not, you can try to put uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE on your manifest and then test it.
you are able to read database file try below code its worked if device is not rooted.
i have tried these in Samsung Grand 2, HTC
String path = Environment.getExternalStorageDirectory()
+ "/WhatsApp/Databases";
File file = new File(path);
if (file.exists()) {
File[] filenames = file.listFiles();
for (File tmpf : filenames) {
// Do something with the files
if (tmpf.isFile()) {
Log.e("tmpf-------->", "not add---->" + tmpf.getPath());
}
}
} else {
Log.e("folder not found", "folder not found");
}
don't forget to add permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
No, You can't get SQLite databse file from an Android device if it's not rooted, since you have no permission to access data folder.
But yes you can get database if you run app on emulator.

Write to a file in SD Card in Android

I tried a simple file write program in Android.
try
{
//File f1=new File("/sdcard/Prag.txt");
File f1=new File("J:\\Prag.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f1,true));
out.write("Pragadheesh" + "\n");
out.close();
} catch (IOException ioe)
{ioe.printStackTrace();}
But the exception is getting hit. How can I fix this?
[edit]:
I tried File f1=new File(Environment.getExternalStorageDirectory() + "//Prag.txt");
and f1 value = /mnt/sdcard/Prag.txt
But still the exception gets hit. I have changed the permission too in manifest file
[solution]
As per Krishnabhadra's reply, I had to Make sure your SDCard is not mounted on a PC
Android is based on linux so windows style paths like "J:\\prag.txt" won't work. In linux paths are like this: "/folder1/folder2/file.txt"
Also don't use hardcoded paths, because different phones might have different path for the SD card. Therefore you you should use Environment.getExternalStorageDirectory() that gives the path to the SD card programmatically.
So use:
File f1=new File(Environment.getExternalStorageDirectory() + "Prag.txt");
Also add necessary permission by:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Inside your manifest, somewhere outside application tag.
All the above answers are useful..But remember one more thing..Make sure your SDCard is not mounted on a PC. Only one device can access SDCard at one time. ie either the computer or the phone can access the SD card, but not both at the same time..If it is mounted on PC then your code won't work..See this thread
Replace:
File f1=new File("J:\\Prag.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f1,true));
with:
BufferedWriter out = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory(),"prag.txt"))
Have you set the required permission in your manifest?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
what is this path "J:\Prag.txt" ; Android doesn't recognize any drive path like system.
Use sdcard path for it.
File f1=new File(Environment.getExternalStorageDirectory()+"/Prag.txt");
Also mention permission in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Categories