android application failed to create directory in SD card - android

I'm trying to create a directory in an SD card using the below code.
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
String secondaryStore = System.getenv("SECONDARY_STORAGE");
File videoDirectory = new File(secondaryStore + File.separator +"video_files");
if(!videoDirectory.exists()){
boolean sd_mkdirs = videoDirectory.mkdirs();
if(sd_mkdirs){
Log.d(TAG,"file Created");
}
}
}else{
File videoDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "video_files");
if(!videoDirectory.exists()){
boolean internal_mkdirs = videoDirectory.mkdirs();
if(internal_mkdirs){
Log.d(TAG,"file Created");
}
}
}
This code checks whether or not the SD card is mounted. If true It creates a directory in the SD card, otherwise in internal storage. However, when it is suppossed to make a directory on the SD card _mkdirs returns false. What am I doing wrong ?
This is my AndroidManifest file. I think I included the permission to write.
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Related

file deletion operation getting failed on the SD card

Android file deletion operation getting failed on the SD card .
following is my code .
File testingfile = new File("file path in SD card");
try{
boolean bool = testingfile.delete();
}
catch(){
Log.d (TAG, e.printmessage());
}
Permissions (already granted ):-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
the file is not getting deleted . the bool always return false.
My buildToolsVersion "28.0.3"

Create folder in internal storage in android using kotlin

I want to create a folder in the internal storage of an android device where I can save my .csv files. But I am unable to create a folder in the internal storage.
Below is my code snippet for creating the folder
fun getStorageDirectory(): File {
if (Environment.getExternalStorageState() == null) {
val f = File(Environment.getDataDirectory().absolutePath + "/Afolder/")
if (!f.exists())
f.mkdirs()
return f
} else {
val f = File(Environment.getExternalStorageDirectory().absolutePath + "/Afolder/")
if (!f.exists())
f.mkdirs()
return f
}
}
and this is the permissions I used in my manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Save pictures into Gallery (internal)

I need to save picture to a Gallery, just to a normal place where the Photos are going. On SO I've found that Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) is internal, no matter that it says ExternalStorage.
But mkdirs always return me false.
String mediaStorage =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Camera";
File mediaStorageDir = new File(mediaStorage);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Toast.makeText(MainActivity.activity, "failed to create directory", Toast.LENGTH_SHORT).show();
return null;
}
}
Is there an easy way to save pics to an internal Gallery directory?
In case, my manifest has :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Android how to create a folder on sd-card?

On my StartActivity, I want to create a Folder for the App on my SD-Card. Now first I set the permission at the manifest.xml like this
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Than I write some code at onCreate Method on my StartActivity like this
//Creat an AppFolder
String appPathString = Environment.getExternalStorageDirectory().getPath() + "/MyIdea";
try {
File appPath = new File(appPathString);
if (!appPath.exists()) {
appPath.mkdirs();
Toast.makeText(getApplicationContext(), "Folder created", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
}
Now I run the app on my Nexus 5 the app start and the toast is showing. But if I go on my device, I can't find any folder which the name, that is beeing created. What is wrong ?
Try this code. It works for me . You need to point to AbsolutePath of storage
String state;
state = Environment.getExternalStorageState();
if ((Environment.MEDIA_MOUNTED).equals(state)) {
File root = Environment.getExternalStorageDirectory();
File Dir = new File(root.getAbsolutePath() + "/My Ideas");
if (!Dir.exists()) {
Dir.mkdir();
}
I'm just wondering, ( I know, we don't write anything) but please give a try to call appPath.flush(); and appPath.close();
and when I'm adding the WRITE permission, I also adding the READ, just to make sure !
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
After a short search i found, that I need special uses-permissions for android sdk 23. So I change my android
<uses-permission-sdk-23
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="23"
/>
Now it works fine. Thanks all for support!

Copy file to external sdcard android

I cannot copy a file to the external sdcard. Does not show up any error, in-fact shows success, but the file is not on the sd card. Code is as follows:
window.resolveLocalFileSystemURI(fileURI, step1,fail);
function step1(tmp_file)
{
file = tmp_file;
window.resolveLocalFileSystemURI("file:///mnt/extsd", step2,fail); //resolve destinaion
}
function step2(destination)
{
file.moveTo(destination,"example.jpg",move_success, move_fail);
}
So on the end it calls move_success.
NOTE: IT WORKS IF I CHANGE PATH FROM 'file:///mnt/extsd' to the internal sdcard path 'file:///mnt/sdcard'
Permissions in manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
You should build your path, not define it as default, cause not in all devices the sdcard is called "sdcard", for example, i have a chinnese tablet, and this has 2 sdcard slots, so i have to use the path "sdcard2" you can do it:
File sdcard = Environment.getExternalStorageDirectory();
String path = "file://"+sdcard.getAbsolutePath();
Then you can use the variable
window.resolveLocalFileSystemURI(path, step2,fail); //resolve destinaio
Or, you can use this method to copy a file:
public void copy(File src, File dst) throws IOException {
try {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (IOException io) {
Toast.makeText(this, "Error: " + io, Toast.LENGTH_LONG).show();
}
}
You can copy the file, next delete it, as a fast move.
Or as other alternative, you can use the property renameTo() of the file,
For example:
File sdcard = Environment.getExternalStorageDirectory();
File example= new File(sdcard.getAbsolutePath()+"/example.txt");
File newpath= new File(sdcard.getAbsolutePath()+"/examplefolder/example.txt");
example.renameTo(newpath);//this will move the file to the new path

Categories

Resources