I am trying to implement the best practices described in Loading Bitmaps effeciently
I've run into trouble because this line:
Utils.getExternalCacheDir(context)
inside of DiskLruCache.java is returning null, which means I get NullPointerException when I try to call .getPath()
Despite the somewhat cryptic NullPointerException that gets thrown the actual issues is that my application did not have WRITE_EXTERNAL permission, so the system was rejecting my attempt to use the ExternalDir for caching. Unfortunately this was happening at a low enough level in the code used in Displaying Bitmaps Effeciently that the Exception does not indicate SecurityException as it normally would if one were trying to write to the SD card without the proper permission.
To fix simply add this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
to your manifest.
This can also happen if you're running on a device or emulator without any external storage.
Related
If I save or load data to or from the internal storage, it could cause an IOException. But I have no clue when this should happen. The only use case could probably be when you run out of space, but is there anything else which could cause this Exception here? (For example some right management configuration on the user side).
And when can this Exception occur while read operation? I don't even know any use case.
You wrote:
The only use case could probably be when you run out of space.
Other cases:
The parent directory does not exist. For example, you're trying to access /data/data/packagename/some_custom_dir_not_yet_created/my_file. You'd need to first call file.getParentFile().mkdirs().
The file you're trying to read does not exist. FileNotFoundException extends IOException.
You app is trying to read / write to another app's internal storage.
first off: I know that this worky only on rooted devices and i know that it is not recommended - but I still want and need to do it.
I am writing an app which performs OCR on an other app, parses its on-screen output and gives the user feedback on the apps progress (therefor using getRootView is out of the question).
This can not be done in an other way, I need to have screenshots of the app at least 3 to 4 times per second.
Other ways that I tried:
/system/bin/screencap - too slow, takes >2 sec per shot on a Galaxy S5.
using obscure C Code to access the internal API of the SurfaceComposer (bloated, did not compile)
What I want: Have a way to read bytes from the framebuffer without having to write it to a file each time.
Currently I have the problem that my app does not have the right permissions. I added the READ_FRAME_BUFFER permission, but I still get a ERRNO 13 (Permission denied) when reading /dev/graphics/fb0, as the app itself is not started with root permissions.
I know I can start a shell or something similar with su, but that is not convenient - I would prefer a way to start a Service or my native Code with the right permissions.
I read about System Services but could not find any "easy" introductions. My experience in C/C++/Java is more than enough, but the Android API's jungle is newfound land to me.
The information from TI-Wiki - Writing System Services seems to implie that a rebuild of the Android System is necessary to integrate a System Service. That would be out of the question. Is that correct?
In an ideal world I would have:
Bitmap Service.getCurrentFrame() {
read one frame from /dev/graphics/fb0
create Java Bitmap
return Bitmap
}
This could be either native or Java code.
But how to gain the privileges?
Any ideas?
Additionally, I read that using the framebuffer is not recommended since it is about to be removed in future releases (sorry, lost source link).
What other fast ways are there to get the current screen content?
Use DDMS lib /dev/graphics/fb0 to get bitmap. No special permission needed.
I have an issue with some devices. I cannot replicate it on any device but I have quite a lot of crash reports reported by some users.
It is this exception:
java.lang.IllegalArgumentException: Unknown URL content://media/external/file
at android.content.ContentResolver.delete(ContentResolver.java:1024)
I use this:
context.getContentResolver().delete(MediaStore.Files.getContentUri("external"),
MediaStore.Files.FileColumns.DATA + "=?", new String[] { path });
After that I call MediaScannerConnection.scanFile() on file's parent directory, because it is imho most functional way how to notify MediaScanner about deletion of file and how to refresh MTP content. I tried all other ways that I have found here on stackoverflow (e.g.Android How to use MediaScannerConnection scanFile) but nothing is working as well as this.
Btw. I use it only for APIs 11 and newer. External storage is certainly mounted.
I have these questions:
1. Do you know any reason why this exception occurs ? I don't want just to ignore the exception. And I don't want to remove this code when it works quite good for most devices.
2. Do you know some new reliable method how to notify MediaScanner and how to refresh immediately content of MTP when some file is deleted ?
Most probably it has to do with caching on the device. Catching the exception and ignoring is not nice but my problem was fixed and it seems to work.
When I attempt to use the solution presented on SO here for dealing with large bitmaps in Android, I get an UnauthorizedAccessException at the point I'm trying to read from the file system from a location such as:
/mnt/sdcard/DCIM/Camera/IMG_20111223_122513.jpg
Is there a particular permission in my manifest I was supposed to select, or something else going on?
Well, there is always this one:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you're looking to display a screen-sized version of a full-resolution photograph, may I please refer you to the Android Training article on this subject.
This is my noob question for the week. I'm looking more for general speculation than specific code and maybe hoping the Android folks are watching and could correct this:
the SDK documentation for Context.openFileOutput says:
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
Ok, that sounds good. I can create a file. Except this method also throws a FileNotFoundException, so apparently something is amiss. Why would a function that is supposed to create a file if it's not found throw an exception if the file is not found???
Kinda defeats that whole "Creates the file..." thing, doesn't it?
I have to apologize for leaping before I looked on this one. I kinda panicked while reading the documentation. After some testing, I found that openFileOutput() does, in fact, work as advertised and will create a file if it's not found, not just throw an FnF exception as I feared. Apparently, the FnF throw was added in case the Activity's application directory does not exist.
Again, my apologies but hopefully, this might help others who are confused by the documentation.
FileNotFoundException is an exception thrown in case that you are trying to write to a file that does not exist, or cannot be currently accessed. When else would this occur?
Perhaps you forgot to close the file, and tried to open the same file.
Perhaps you tried to create multiple FileOutputStream objects pointing to the same file.
These will result in a FileNotFoundException.
Anyway, you can insert a throws FileNotFoundException at the end of your function declaration where you call openFileOutput (and to other functions that call this function).
Possibly also thrown if you use MODE_APPEND, which appends to an existing file and the file doesn't exist.
Have you inserted the right Permission?
see http://developer.android.com/reference/android/Manifest.permission.html
for reference.
i think
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
could be the one You are fine with