file deletion operation getting failed on the SD card - android

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"

Related

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

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

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!

SecurityException: Permission Denial on reading file from Downloads

There are some similar questions on StackOverflow but the situations they happened a little bit different.
I need to read a file (in Service) that user shared with a standard share mechanism.
There are no problems reading files from gallery or files shared by File Managers or Google Drive. But there are problems when I try to share files from Downloads.
I receive:
java.lang.SecurityException: Permission Denial: opening provider
com.android.providers.downloads.DownloadStorageProvider from
ProcessRecord{432abf50 8550:clipboard.clipboardtest/u0a167} (pid=8550,
uid=10167) requires android.permission.MANAGE_DOCUMENTS or
android.permission.MANAGE_DOCUMENTS
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="virtualclipboard.copytodesktop"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.ACCESS_ALL_DOWNLOADS" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" />
<application>
...
</application>
</manifest>
The code to read a file. I receive Uri in intent while share operation.
InputStream input = getContentResolver().openInputStream(fileUri);
byte[] fileData = Converter.InputStreamToByteArray(input);
// ...
public class Converter {
public static byte[] InputStreamToByteArray(InputStream inputStream) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int reads = inputStream.read();
while(reads != -1){
baos.write(reads);
reads = inputStream.read();
}
return baos.toByteArray();
}
catch (Exception e) {
return null;
}
}
}
Reproduced on Nexus 5, 4.4.4, KitKat.

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