How to use setImageDrawable() with image paths. ANDROID - android

I am trying to put an image in an imageView by code. I have some images in the Drawable directory but, with other images, I have to use the internal storage. In first case,
holder.picture.setImageDrawable(getResources().getDrawable(R.drawable.my_image));
works but, in the second case, Android give me this error:
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Sensia_Sniffer/18_04_2018/18_56_18_photo.JPEG (No such file or directory)
and I don't know why. I am using this lines:
Drawable drawable = Drawable.createFromPath(my_image_path);
holder.picture.setImageDrawable(drawable);
and in AndroidManifest.xml I have this permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Could you help me with this?
Thank you very much!!!

You forgot to ask for runtime permissions for the requested permissions in manifest file.
Needed for Android 6+.

Related

Android Kotlin - Can not delete file from download folder

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"

PDF.js does not open pdf file android

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.

How to delete files from media content providers on android N

This code worked on previous versions of android, but no longer seems to work on N.
I want to delete a media file, given the content URI of the file. I query the content provider for the path of the file. On android N, this seems to return a filename of the following form:
/storage/1143-1403/DCIM/Camera/20171019_185604.mp4
On previous versions of Android, the path would been a direct path to the SD Card. (And the real file can actually be found in the /sdcard/DCIM/Camera directory).
The problem: when I call new File(path).delete(), the delete fails. It succeeds in previous versions of Android.
Manifest permissions are:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
I have requested the following permissions, and they have been granted:
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
...
Manifest.permission.MODIFY_AUDIO_SETTINGS,
There is no Manifest.permission flag for WRITE_INTERNAL_STORAGE.
I'm guessing "/storage/1143-1403/DCIM/Camera" is some kind of fused file system.
Calling file = file.getAbsoluteFile().getCanonicalFile(); does not change the path or otherwise improve the situtation.

How to display html page kept on sdcard on webview

I am not able to display html page kept in sdcard on webview. I have tried using this two methods:
webPage.loadUrl("file://"+ Environment.getExternalStorageDirectory()+"/page1.html");
and
webPage.loadUrl("content://com.android.htmlfileprovider"+
Environment.getExternalStorageDirectory()+"/page1.html");
Please Guide.
Try This.
webPage.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/page1.html");
You forgot to add slash.
Add Internet Permission and Read External Permission.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Android Permissions Issue

I am writing a simple activity to record and save audio, preferably to a folder within my application, but, for simplicity, to the SD card. The line of code that's giving me trouble is
String path = Environment.getExternalStorageDirectory().toString() + "/" + "tempAppFiles/";
String filename = "test"+".mp4";
recorder.setOutputFile(path + filename);
where recorder is an instance of MediaRecorder.
When I run the application, I get a permissions error that states
07-31 15:51:51.810: W/System.err(13670): java.io.FileNotFoundException: /mnt/sdcard/tempAppFiles/test.mp4 (Permission denied)
I looked this problem up and found that I needed to add several permission tags to my manifest, and I added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
to my manifest.
I am still getting the same permissions issue, and I can't find anyone with a similar problem.
Any ideas?
Hmm... The permissions seem to be correct, perhaps you've put them in the wrong place, they need to be children of the root note .
Next you can check, whether you can write the file by checking
boolean canIWrite = path.canWrite();
As you also get a FileNotFound exception you could try...
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
...instead of...
String path = Environment.getExternalStorageDirectory().toString();
If that is no help at all, there is still an official example of capturing audio - you should compare your code to:
http://developer.android.com/guide/topics/media/audio-capture.html
tempAppFiles does not set well with me. Seems like you should be writing to a directory that is under the package name of the app you are writing ...

Categories

Resources