I am trying to read local pdf file which is saved in Android download folder.
I created app and set everything like it should be and app reads pdf if it is saved in asset:
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/abc.pdf")));
But if I use:
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///storage/emulated/0/Download/foo.pdf")));
nothing is showing.
I checked if file exist:
string abc;
if (File.Exists(string.Format("/storage/emulated/0/Download/foo.pdf")))
{
abc = "exist!!!!";
}
else
{
abc = "not exist!";
}
and it confirmes file exist.
If i use OpenPdf project: https://github.com/acaliaro/OpenPdf
then it can open file saved in Download folder.
What am I doing wrong? Different Android version? Api level? I compared OpenPdf project with my project, everything seems to be ok.
Permisions are set too:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
For everybody who has similar issue:
Solution is very trivial.
Apart from permissions adjusted in AndroidManifest.xml I had to go to phone aplications settings and turn on memory permission manualy.
Everything is working now.
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"
First and foremost, I'm testing this app using my Google Pixel XL (1st gen 32 GB version, runnning Android 9 API Level 28)
Basically, I'm trying to make a camera application based on Android Camera 2 API (I just downloaded the sample code from https://github.com/googlesamples/android-Camera2Basic). and I'm stuck because I'm trying to save the picture taken by the camera into "Pictures" folder inside "/storage/emulated/0/".
However, for some reason even though it seems like I'm able to do this, the folder and the picture file are not there when I checked them with ES File Explorer.
I've tried 3 methods so far:
Attempt #1:
mFile = new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "newPicture.jpg");
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
mFile.mkdirs();
}
Attempt #2:
File filePath = new File(Environment.getExternalStorageDirectory().getPath(),"randomFolder");
if(!filePath.exists()){
filePath.mkdirs();
}
String fullPathName = filePath.getAbsolutePath() + "/" + "newPicture.jpg";
mFile = new File(fullPathName);
Attempt #3:
mFile = new File(fullPathName);
File filePath = new File(Environment.getExternalStorageDirectory() + "/Pictures/");
if (!filePath.exists()){
filePath.mkdirs();
}
mFile = new File(filePath,"newPicture.jpg");
mFile is the File path where "newPicture" will be saved to, basically it's supposed to be saved to "/storage/emulated/0/Pictures/newPicture.jpg"
Now, I've tried to use getExternalFilesDir() method so the picture will be saved into /storage/emulated/0/Android/data/com.***.***[the package name]/files/Pictures/newPicture.jpg" and in this case, IT TOTALLY WORKS! However I REALLY want to save the picture to /storage/emulated/0 directory because I want user to be able to access the data easily later on.
In order to illustrate this better, I've recorded the application running and then took a picture and saved it in a gif, uploaded it to imgur right here:
https://imgur.com/Szpc1HR
See that? The picture seems to be saved into "/storage/emulated/0/Pictures/newPicture.jpg" successfully but then when I went to ES File Explorer to check the picture, the folder "Pictures" was not created, nada.
I'm literally on my wits end right here haha, does anyone know a solution to this problem? Any advice will be greatly appreciated
P.S. Almost forgot to say, I've already added the permission in AndroidManifest.xml:
<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" />
As someone said in this post:
how-can-i-access-storage-emulated-0-dcim-on-android-device
,
The "/storage/emulated/" folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored. You'll need to find the actual physical location on your device where it is stored.
Maybe this link will help, Or this Can't create a directory on /storage/emulated/0 on emulator.
I tried to change the file name of a song by programmatically by using the code
File from = new File(filedirectory,currentName+type);
File to = new File(filedirectory,newName+type);
from.renameTo(to);
But after executing the code the current file is not show in the music list, also the new one is not showing in the list.
The file path and the file name are:
filedirectory: /storage/emulated/0/media/audio/music/
newName: aaaa.m4a
currentName: bbb.m4a
In the manifest I given all the necessary permission and am executing the code in Kitkat.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
After rename the file, I checked whether the file exists or not by pragmatically, the result is file not exists with the new name.
Have you ever tried to use "adb shell" and "cd /storage/emulated/0/media/audio/music/", then list file and check the files there? Whether the filename has been changed or not?
If the filename changed, then it is an issue of sync in the Music Player.
I have ever met issue that I changed the photo album, but cannot get the changes in my App by get system photo album through content provider. I have no idea how to solve it, and it disappears after phone restarted.
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 ;).
This is the previous post.
About mp3 player
And the picture below is my A.mp3 path I find in my phone.
`
mediaPlayer.setDataSource("/storage/sdcard1/A.mp3")
File file =new File(Environment.getExternalStorageDirectory(),"A.mp3");
mediaPlayer.setDataSource(file.getPath());
There are two paths above..According the picture,it should be the first one,but it does not work.
I push A.mp3 into the internal storage,and play is ok.
mediaPlayer.setDataSource("/system/A.mp3");
I finally find that the two files got different permission.I don't know what do those "wrdrrwrwrwrw*****" mean.So I need to search it .
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
This permission was forgotten.Now is ok
Firstly- Check that the SD card is mounted and readable.
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
or
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY));
From your question here: You are using:
File file =new File(Environment.getExternalStorageDirectory(),"A.mp3");
mediaPlayer.setDataSource(file.getPath());
and have tried:
mediaPlayer.setDataSource("/storage/sdcard1/A.mp3")
Then try:
Edit: add in Music folder.
mediaPlayer.setDataSource("/sdcard1/Music/A.mp3");
Also: this http://www.bogotobogo.com/Android/android24Media.php#SDCard is an interesting link. So you can explore you files in Android Studio.