Create folder in internal storage in android using kotlin - android

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" />

Related

Writing file in USB Drive in android

I am trying to copy a file from my app to USB Drive(Pen drive) in Android but I am getting the Permission denied.
I have already given runtime storage permission.
Process: com.testapplication, PID: 19261
java.io.FileNotFoundException:
/mnt/runtime/write/02EE-0EE1/Tele-calling list.xlsx (Permission
denied)
at java.io.FileOutputStream.open0(Native Method)
Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
Code
val cacheFile = File(cachepath, it.name)
val pendriveFile = File(pendrivepath, it.name)
Log.d(TAG, "********: " + cacheFile.path)
Log.d(TAG, "&&&&&&& " + pendriveFile.path)
cacheFile.copyTo(pendriveFile)
Toast.makeText(context, it.name + " copying..", Toast.LENGTH_SHORT).show()
I am facing issue in cacheFile.copyTo(pendriveFile) line.
Pendrive path is "/storage/02EE-0EE1" that I have calculated in runtime.

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"

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 application failed to create directory in SD card

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" />

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