I am downloading file from a server in Android using the DownloadManager class. I want to store this file in the internal memory of the device. I tried to use .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/") as mentioned here, but it is not working. How to solve my problem?
add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to manifest.xml and create "/Android/data/xxx.xxx.xxx/files/" path
try this
File testDirectory = new File(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/");
and
if (!testDirectory.exists()) {
testDirectory.mkdirs();
}
Related
I am unable delete txt file from download directory. File.delete() does not work. I have also tried with context.deleteFile(), that does not work either. I am not getting any exception, just nothing happens. This works on many Android devices, but it does not work on the device that I needed it to work, which is Android device with embedded printer. Android version 8.1.
I have found this answer, stackoverflow, which is similar to official documentation Google.
I have tried to use Uri.fromFile(directory) and directory.toUri()
instead of MediaStore.Images.Media.EXTERNAL_CONTENT_URI from the example, since file is in download folder, but I got null pointer exception.
How I can get correct download folder Uri? Or is there another way to delete file?
Thank you in advance for any help and suggestions.
EDIT:
getAbsolutePath() = /storage/emulated/0/Download/print.txt
Code:
val downloadPath: File = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
// dowloadPath is passed as directory
fun readFile(directory: File, context: Context): String {
val fileToRead = File(directory, "print.txt")
//...
if (fileToRead.exists()) {
//...
fileToRead.delete()
}
}
Permissions:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />
And inside <application
android:requestLegacyExternalStorage="true"
android:requestRawExternalStorageAccess="true"
I am sorry I am extremely new to android and I am lost. I successfully found out how to save a file on an android system, but every time I try to search for the file on my Android phone (which is where I have a copy of this app located) I am unable to locate it. I know it is there because the app would not start up with out it. How do you write a file that can be both used by the App AND searched by the user in the File Explorer. I am using a Galaxy Note 3 Android version 5.0 Any help would be appreciated.
private void WriteFile(String FileName, String FileCont, boolean isAppend){
File file = new File(getApplicationContext().getFilesDir().getAbsolutePath() + "/" + FileName);
try {
FileOutputStream stream = new FileOutputStream(file, isAppend);
stream.write(FileCont.getBytes());
stream.close();
}
catch(Exception ex){
}
}
Did set the permissions to write on external space in your manifest.xml?
For reference see
http://developer.android.com/reference/android/Manifest.permission.html
You have to set the "WRITE_EXTERNAL_STORAGE" permission to write on external disk. Just add the following line to your android manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
It will implicitly contain the permission to read external storage as well ;).
I am creating an Android app in which you copy an image from location x to location y. After the copying is complete I would like to see the picture in a ImageView.
I know the images location, but no matter what I try I can't create a bitmap object of it.
The line which are causing my problems is this:
BitmapFactory.decodeFile(dir+s);
dir = getCacheDir().getAbsolutePath()+"/images/";
s = file name (eg. 1275123123.jpg)
If I create a File object with the same path, and call f.isFile(), it returns true.
Opening the image in either android or windows are not a problem.
Didn't you forget to add SD card access permissions READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE ?
I had a similar problem and what eventually solved it was creating FileInputStream(...) and decoding the stream. This is the code I used, I changed it to suite your case:
File location = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/images");
File dest = new File(location, fileName + ".JPG");
FileInputStream fis;
fis = new FileInputStream(dest);
Bitmap img = BitmapFactory.decodeStream(fis);
You have to add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
permissions to you manifest.
And please don't forget to request runtime permission from user.
I want to create a file(not created) in a directory(not created) in the SDCARD.
How doing it ?
Thank you.
Try the following example:
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//handle case of no SDCARD present
} else {
String dir = Environment.getExternalStorageDirectory()+File.separator+"myDirectory";
//create folder
File folder = new File(dir); //folder name
folder.mkdirs();
//create file
File file = new File(dir, "filename.extension");
}
Don't forget to add the permission to your AndroidManifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The problem is that mkdirs() is called on a File object containing the whole path up to the actual file. It should be called on a File object containing the Path (the directory) and only that. Then you should use another File object to create the actual file.
You should also have to add permission to write to external media.
Add following line in the application manifest file, somewhere between <manifest> tags, but not inside <application> tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Use This:
DocumentFile tempDir = DocumentFile.fromSingleUri(context,targetDirectoryUri);
DocumentFile newDocumentFile = tempDir.createFile(type,name);
"targetDirectoryUri" is uri of the directory you want to put the file into it.
This is the only solution!
After Api 19 you can not write on SDCard, so you must use DocumentFile
instead File.
In addition, you must also take SDCard permission. To learn how to do this and get the targetDirectoryUri, please read this.
You can use in Kotlin
val file = File( this.getExternalFilesDir(null)?.getAbsolutePath(),"/your_image_path")
if (!file.exists()) {
file.mkdir()
}
Don't forget to give permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
See here why creating files on root of the SD card is a bad idea
I'm new to Andriod development and was wondering how I could retrieve the 3gp file from an SD card into my application?
try this (one of the many way to do that)
String FilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/your/file/directory"+ "YOURFILENAME";
File 3gpFile = new File(FilePath);
now go ahead and use 3gpFile to do other stuff
remember to have uses-permission in your manifest file
<uses-permission android:name"android.permission.WRITE_EXTERNAL_STORAGE"/>