I tried a lot to find a solution but everything has failed so far. It's probably something stupid.
I'm trying to save a photo as a jpeg in the storage of the phone. Everything fails even the simple task of making a folder. The code below is what I use to create the folder.
private void makeFolder(){
try{
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),getString(R.string.app_name));
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
Toast.makeText(this,"Done",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"folder_failed",Toast.LENGTH_SHORT).show();
System.out.println(folder.getAbsolutePath());
}
}catch (Exception e){
Toast.makeText(this,"exception",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
I have tried many different ways like:
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + getString(R.string.app_name)+File.separator);
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + getString(R.string.app_name);
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
,getString(R.string.app_name));
I have tried both mkdirs and mkdir and
-Environment.getExternalStorageDirectory().getAbsolutePath()
-getExternalStorageDirectory().getAbsolutePath()
-Environment.getExternalStorageDirectory()
-getExternalStorageDirectory()
The permission exists in the manifest and is accepted.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The path the the System.out prints is "/storage/emulated/0/appname"
I don't know what else to try. Similar code works for other applications. Min API is 23.
Any help is much appreciated.
I suggest you to try Context.getExternalFilesDir() instead of Environment.getExternalStorageDirectory()
If your test device runs Android 10/Q or API Level 29+: there are a few permission changes with storing data to media folder. You can store your data without problems to your app folder, but if you remove the app the images are gone too.
The easiest way is to add android:requestLegacyExternalStorage="true" in the manifest under application but it is more like a work around and it will probably not be supported for long.
Search for "store image android 10" here and you find the right way for you.
Related
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();
}
Before It gets brought up about my question already being asked, I would like to state that I have tried around 5 other options and possible solutions with no result.
Here is a snippet of my code. This is just a snippet. Upon testing the results of my code currently, a file is being saved in the main directory, /ScoutingApp. However, I would like to files to save in a folder /ScoutingApp/ on the MicroSD card so I can eject data more quickly.
if (Environment.MEDIA_MOUNTED.equals(state)) {
File root = Environment.getExternalStorageDirectory();
File Dir = new File(root.getAbsolutePath() + "/ScoutingApp");
if (!Dir.exists()) {
Dir.mkdir();
} else {
filename = UUID.randomUUID().toString() + ".sql";
File file = new File(Dir, filename);
If the Android that your Fire OS is based on is Android 4.4+, you can try getExternalFilesDirs() on any Context (such as an Activity). Note the plural form — if this method returns 2+ items, the second and subsequent ones are on removable storage. Those locations will be specific for your app, and you can read from and write to those locations without permissions.
Note, though, that Fire OS is not completely compliant with the Play ecosystem's compatibility requirements, and so YMMV.
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.
I want to get a file which i saved in a specific directory on my phone.
How can I find and get a ref to it so I can do with it something different like uploading to a server?
Thanks in advance.
Shiran
Well where do you store your file?
Store the file in a specific area such as
Environment.getExternalStorageDirectory()
Then when you want to read it, just use the file name used and the path obtained from the above method.
File f = new File("location");
Remember, if you're trying to access the sd card you need to set permissions. Then just handle the "file" in whatever way it is you're trying to do something.
If you're looking to use the SDCard check this post:
Permission to write to the SD card
[Edit - Example]
String filenameToWrite = "Test.jpg"
try{
File f = new File(Environment.getExternalStorageDirectory() + "/Photos/");
if (f.exists()){
File fileToWrite = new File(Environment.getExternalStorageDirectory() + "/Photos/" + filenameToWrite;
// Do something to write file, save bitmap, save byte stream, etc.
}
} catch(IOException e){
Log.e("File", "Could not save file, error occured: " + e.toString());
}
Here's a few getting started tutorials to help you understand how to read and write files in java. This (apart from the 'path') is a java question, not an Android one.
Tutorials:
http://beginwithjava.blogspot.com/2011/04/java-file-save-and-file-load-objects.html
http://www.roseindia.net/java/beginners/java-write-to-file.shtml
Im trying to build a directory called "images" on the SD card on android. This is my code but its not working? Can anyone give me some advice?
File picDirectory = new File("mnt/sdcard/images");
picDirectory.mkdirs();
Update: Since Android 10,11 Storage updates, Google has restricted Storage access through standard programming language file operations.
For applications targeting only Android 10 (API 29) and above, you need to declare "requestLegacyExternalStorage="true" " in your android manifest file to use programming language based file operations.
<application
android:requestLegacyExternalStorage="true"
....>
==========
You want to be sure you are correctly finding the address of your SDCard, you can't be sure its always at any particular address. You will want to do the following!
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"images");
directory.mkdirs();
Let me know if this works for you!
You will also need the following line in your AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I use this to know the result:
File yourAppDir = new File(Environment.getExternalStorageDirectory()+File.separator+"yourAppDir");
if(!yourAppDir.exists() && !yourAppDir.isDirectory())
{
// create empty directory
if (yourAppDir.mkdirs())
{
Log.i("CreateDir","App dir created");
}
else
{
Log.w("CreateDir","Unable to create app dir!");
}
}
else
{
Log.i("CreateDir","App dir already exists");
}
you can use this :
File directory = new File(Environment.getExternalStorageDirectory() + "/images");
directory.mkdirs();
Environment.getExternalStorageDirectory() is deprecated. So you should use this:
File directory = new File(this.getExternalFilesDir(null).getAbsolutePath() + "/YourDirectoryName");
directory.mkdirs();
One thing that is worth noting is if you always get false from the mkdirs(), try to unplug your device from pc, and see if it could create folders. At least I tried, it worked for me, currently I'm looking for ways to fix this problem.
To create specific root directory and its sub folder i use this code
String root = Environment.getExternalStorageDirectory().toString();//get external storage
File myDir = new File(root + "/grocery"+"/photo/technostark");//create directory and subfolder
File dir=new File(root + "/grocery"+"/data");//create subfolder
myDir.mkdirs();
dir.mkdirs();
To create file inside sd card you have to use Environment.getExternalStorageDirectory()
/**
* Creates a new directory inside external storage if not already exist.
*
* #param name The directory name
*/
public static void createNewDirectory(String name) {
// create a directory before creating a new file inside it.
File directory = new File(Environment.getExternalStorageDirectory(), name);
if (!directory.exists()) {
directory.mkdirs();
}
}
Following two important parameter which helps you to create directory
1. directory.mkdirs() :
Creates the directory named by this file, creating missing parent
directories if necessary.
2. directory.mkdir() :
Creates the directory named by this file, assuming its parents exist.
For more you can how getExternalStorageDirectory() works please see link
This should help.
First get the path of the external storage:
File root=Environment.getExternalStorageDirectory();
Then:
File picDirectory = new File(root.getAbsolutePath(), "mnt/sdcard/images");
picDirectory.mkdirs();