Android - how to read screenshot without gallery intent - android

I am interested in writing a screenshot app and want to learn the technique from this app.
After user takes a screenshot using power and volume buttons, the app opens up the screenshot without the user needing to pick an image from the gallery. I want to do something similar (save the user the step to navigate the gallery to get screenshots).
Does anyone how can an app read a screenshot as this app did ? (In the app's demo video this step is shown at time 0:30)
Edit
I've tried testing it on my nexus 5. I can see the screen shots are in folder /sdcard/Pictures/Screenshots. The directory permissions are:
drwxrwx--x root sdcard_rw 2015-08-30 01:42 Screenshots
I gave my app storage permissions. I used the following code in a service, but it didn't work:
FileObserver fileObserver = new FileObserver("/sdcard/Pictures/Screenshots") {
#Override
public void onEvent(int event, String path) {
Log.d("Test", "FileObserver event");
}
};
fileObserver.startWatching();

It should be running as a service in the background, which register a FileObserver, and perform action upon file added.
Or the service simply check the folder manually.
Edit:
Warning: If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.
It means that local variable is definitely not working, put it into a field AND make sure your class will not be garbage collected, e.g. even Activity can be killed, and garbage collected afterwards.

Related

Changing how android opens files so that it triggers a system service function

Where in AOSP code would I look to add code that triggers a custom system service whenever the user attempts to open a specifically named file?
For example, if a user opens a file on Microsoft Excel on Android, I'm assuming the application is creating a fileinputstream to read in the spreadsheet.
I followed the instructions on http://processors.wiki.ti.com/index.php/Android-Adding_SystemService
All files open by any process are reflected in /proc. I would either modify the procfs to get immediate and full track of such events, or if the requirements allow, retreat to a less penetrating approach, monitoring /proc once in a short while.
See also How do I monitor opened files of a process in realtime?.

how can I input information data when app is in first startup?

I want input data in internal storage of my app when my app installed in new device & never change(delete & update &insert). my information is heavy & if I input my information in oncreate() every time that user open my app information will be make again & I don't like it.how can I do it?
Create a class that is a Broadcastreceiver and register it in your manifest to receiver Intent.ACTION_PACKAGE_INSTALL actions. It will be called only when the application is installed.
You can use assets folder and put your heavy data there. It will be installed with application package. You can find information about that here.

Listen to photo taken event

Just want to register to video/photo taken events.
Hopefully, but not a must, the event will be triggered when the operation have finished, so I won't process half photos or half taken videos.
Already tried:
fileObserver = new FileObserver(dcimDir, FileObserver.ALL_EVENTS)
I see the events when traversing using a file manager app but not when a picture is taken nor when copying.
Ideas?
Found a way by registering to all sub directories of dcim (except the ones that start with a period):
new FileObserver(dcimDir.toString(), FileObserver.CLOSE_WRITE)
The reason I need to register to all of them is because different phones put picutes and videos in different folders - at least they are all under DCIM.
The reason the event is 'CLOSE_WRITE' is because I want to trigger after the photo/video is complete, so I won't process only half of the photo/video.
There is another way:
ContentProvider
See another SE thread for more info: Android -- How does Google+ instant upload work?

how to run application in background in android?

Hi, I want to make an Android application that continues to run in background and when user accesses any folder, picture, or any other file it notifies using toasts that he accesses this file(filename).
The other people answering your question are focused on the "background" part, and a Service would indeed accomplish this. Users have fairly loudly stated that they despise constantly-running services like the one you are proposing.
when user access any folder or picture or any file it notify using tosts that he acess this file(filename).
For files that you can access yourself (e.g., those on external storage), you can use FileObserver class.
Use FileObserver to detect file access or change.

Android: Problem with Media Scanner not Running

I have an app that user's can draw with, and then 'export' that drawing as a .png file to external storage, if present. Generating the PNG, copying the file to external all work like a charm, but a rather unique problem happens; after the export, if the user navigates to the image via My files (Samsung Tab running 2.2 in this case), they can see the .png file, but when they open it, the screen is black for about 10 seconds... then they see the image, Additionally, the images don't show up in the user's 'Gallery' app either.
Now, if the user connects the device to the computer via USB, or reboots the device, they can access the images no problem from My files, and they appear in 'Gallery' from that point forward, but again, any newly esported files experience the same problems until they cycle/connect the device again.
My thinking was that this had to be related to the Media Scanner (at least in the case of the 'Gallery' problem, it most certainly is).
So, as I am targetting Api 8+, I am trying to use the static MediaScannerConnection.scanFile() method to have the OS re-scan and add my images into the Gallery, etc. Also hoping this solves the issue of the strange delay in opening the images. Here is my code:
MediaScannerConnection.scanFile(
context,
new String[] { "/mnt/sdcard/MyApp" },
null,
null
);
LogCat gives me the following entries when I export an image, and thus run the above call:
DEBUG/MediaScannerService(2567): IMediaScannerService.scanFile: /mnt/sdcard/MyApp mimeType: null
DEBUG/MediaScannerService(2567): onStartCommand : intent - Intent { cmp=com.android.providers.media/.MediaScannerService (has extras) }
DEBUG/MediaScannerService(2567): onStartCommand : flags [0], startId [1]
DEBUG/MediaScannerService(2567): ServiceHandler:handleMessage volume[null], filePath[/mnt/sdcard/MyApp]
DEBUG/MediaProvider(2567): getSdSerial() sd state = removed
INFO/Database(2567): sqlite returned: error code = 17, msg = prepared statement aborts at 43: [SELECT DISTINCT sd_serial FROM images WHERE sd_serial LIKE 'external_0x%']
ERROR/MediaProvider(2567): removeMediaDBData called
DEBUG/MediaScanner(2567): prescan enter: path - /mnt/sdcard/MyApp
DEBUG/MediaScanner(2567): prescan return
So, it looks like the MediaScanner is getting the correct location, but is failing to find the SD card, which is correct, and failing. The Samsung Tab has built-in non-SD external storage, which Android gives access to via Environment.getExternalStorageDirectory(). How do I tell it to scan the non-SD storage?
Any ideas how to proceed?
Paul
Found the solution here, which involves sending a broadcast request to the media scanner via an Intent:
How to update the Android media database
Never did figure out the issue with MediaScannerConnection.scanFile.
Whenever you add a file, let MediaStore Content Provider knows about it using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAddedOrDeleted)));
Main advantage: work with any mime type supported by MediaStore
For deletion: just use getContentResolver().delete(uri, null, null)

Categories

Resources