How to trigger broadcast receiver after an image is deleted in Gallery? - android

I am working on an android app that is supposed to have copy of deleted image from android gallery. If the user wants to delete a picture from gallery, before he/she delete it, our app must save a copy of the image in a hidden folder.
I used SMS receiver but this one is some thing confusing.
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
//activity which I want to perform here is to have a copy of deleted image in a hidden folder
}

You canot track a file when it is deleted. You can use file observer for track other cahnges in a folder.
observer = new FileObserver(Your_folder_Path) { // set up a file observer to watch this directory on sd card
#Override
public void onEvent(int event, String file) {
//if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
Log.d(TAG, "File created [" + pathToWatch + file + "]");
Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
//}
}
};
observer.startWatching();

Related

file chooser to choose folder (android)

i am using afilechooser for this purpose . and this is by default programmed to choose the items inside the folder and get you the path that is selected by the user.
but i want to use this as folder chooser where the user choose a location from internal memory of android device and then the app will save the file at that location.
so how do i do it.
the code i am using for this purpose is-
private void showChooser() {
// Use the GET_CONTENT intent from the utility class
Intent target = FileUtils.createGetContentIntent();
// Create the chooser Intent
Intent intent = Intent.createChooser(
target, getString(R.string.chooser_title));
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// The reason for the existence of aFileChooser
}
}
and i suspect the code can be changed to choose the folder instead of files. any suggestion can be helpful . please suggest if any other way to achieve what is want .
thank you
Looking at the github project in the url you posted, it doesn't look like this can be achieved. My claim is based on the following piece of code inside com.ipaulpro.afilechooser.FileChooserActivity class:
#Override
public void onFileSelected(File file) {
if (file != null) {
if (file.isDirectory()) {
replaceFragment(file);
} else {
finishWithResult(file);
}
} else {
Toast.makeText(FileChooserActivity.this, R.string.error_selecting_file,
Toast.LENGTH_SHORT).show();
}
}
just look at the if(file.isDirectory()) statement.

Tracking changes on /proc/uid_stat/myAppUID

Does someone know is there a way I can get notified on file change in /proc/uid_stat/myAppUID folder?
I want to track data usage of my app. The file is there and when I read it manually using BufferedReader, I get the data traffic.
I tried using FileObserver class and also RecursiveFileObserver but I don't get any callback when data usage change. My guess is that it doesn't work on virtual file system. I would like to get notified from linux when these files changes, because looping constantly through files is not a valid option for me.
Here is the code I used:
path = "/proc/uid_stat/"+getApplicationInfo().uid;
observer = new FileObserver(path) {
#Override
public void onEvent(int event, String file) {
Toast.makeText(getApplicationContext(), file + " was changed!", Toast.LENGTH_LONG).show();
}
}
};
observer.startWatching();

Instabug, is it possible to deactivate the shake for feedback, if there is no internet access?

I have a networkStateReceiver, that checks if I have internet or not.
If I do, I reinitiate instabug, if not, I want to deactivate. How can I do that?
I tried just setting it as null, but it doesn't work.
if(haveConnectedMobile || haveConnectedWifi){
//TODO will need to make a queue, and go through all that queue
PSLocationCenter.getInstance().initInstabug();
}else{
PSLocationCenter.getInstance().instabug = null;
}
This is my init:
public void initInstabug() {
String[] feedbackArray = getResources().getStringArray(R.array.feedback);
String randomStr = feedbackArray[new Random().nextInt(feedbackArray.length)];
Instabug.DEBUG = true;
instabug = Instabug.initialize(this)
.setAnnotationActivityClass(InstabugAnnotationActivity.class)
.setShowIntroDialog(true, PSTimelineActivity.class)
.enableEmailField(true, false)
.setEnableOverflowMenuItem(true)
.setDebugEnabled(true)
.setCommentRequired(true)
.setPostFeedbackMessage(randomStr)
.setPostBugReportMessage(randomStr) //TODO will be the post report message, random from array
.setCommentFieldHint("Please describe what went wrong")
.setPreSendingRunnable(new Runnable() {
#Override
public void run() {
String[] files = new String[2];
files[0] = Environment.getExternalStorageDirectory() + "/Passenger/passenger_log.txt";
files[1] = Environment.getExternalStorageDirectory() + "/Passenger/passenger_log2.txt";
Compress compress = new Compress(files, Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
compress.zip(new CrudStateCallback() {
#Override
public void onResponse(String string) {
Log.i("", "ended making the archive");
}
});
}
})
.attachFileAtLocation(Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
}
You can use this code to disable Instabug automatic invocation:
Instabug.getInstance().setInvocationEvent(IBGInvocationEvent.IBGInvocationEventNone)
This way it won't be invoked automatically. This will only affect the next Activity though (not the current one). You may force to stop and restart all listeners by calling onPause and onResume on the current Activity. (We may address that soon though, so that such changes are applied on the currently running Activity).
Don't forget to also enable the shake invocation event when internet access is restored.
Please keep in mind that Instabug SDK already caches all reports and will re-attempt to send them on next app launch until they're uploaded successfully.
Just wanted to post the updated answer.
The newer SDK has changed the name and now you can disable it by the following code:
Instabug.changeInvocationEvent(InstabugInvocationEvent.NONE)
Notice, if you want to disable it for entire application, just call this method in your Application class

Android - mount OBB in service

I'm writing an application which includes a service and mounts an OBB file from service.
I'm referring this documentation: http://developer.android.com/reference/android/os/storage/StorageManager.html
http://developer.android.com/reference/android/os/storage/OnObbStateChangeListener.html
However, onObbStateChange() callback never happens.
If I use the very same code in application's activity, it successfully mounts and I get access to the internals of the OBB file.
Below is the code snippet:
// Extend OnObbStateChangeListener class
public class MyService extends Service {
static StorageManager storageManager;
static class Observer extends OnObbStateChangeListener {
#Override
public void onObbStateChange(String path, int state) {
Log.v(TAG, path + " " + state); // 1
}
}
}
// Mount obb
public void getMountedPath() {
File file = new File("<path to obb file>");
if(!obbFile.exists())
return;
Observer observer = new Observer();
boolean state = storageManager.mountObb(obbFile.getAbsolutePath(), null, obbObserver);
Log.v(TAG, "state: " + state); // 2 It always prints TRUE
}
Any idea why it's not able to mount from my service class?
More observations:
When I execute the app for the first time, I get mount state as TRUE in the second log statement and onObbStateChange() doesn't get invoked.
However, when I run my app second time, I see the first log from onObbStateChange() and it prints state as 24 which means ERROR_ALREADY_MOUNTED.
So it seems that the obb got mounted in the first attempt, however, onObbStateChange() didn't get invoked for some reason.
Any explanations on this behavior?

App crash when directory does not exist

Hi I am fairly an amateur at android so I might not be realizing something obvious.
I have a method that populates a global File Array variable with a list of flies in a specific directory. Problem is everything works fine if the directory has been made before by using my app to save a file there however when the user hasn't done that an error message is suppose to pop up saying they haven't saved a file yet.
I do a check if the directory exist but the app crashes when the directory has not been created.
This is what my code looks like any assistance would be appreciated
private void getTemplates()
{
//Gets file directory for saved templates
File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");
//Checks if path exists in other word if any templates have been saved before
if(finalMarkTemplateDir.exists())
{
templatePaths = finalMarkTemplateDir.listFiles();
}
else
{
Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();
setResult(RESULT_CANCELED);
finish();
}
}
I am too an amateur, you have not created a file in your code, calling a new file() method does not create a file. Pls check that out
try {
finalMarkTemplateDir.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I managed to solve my problem when I call the setResult and finish methods I did not realize the flow of the program is returned to my onCreate method which meant the rest of my method calls in onCreate was still being called and they require the templatePaths array.
So basically I thought finish would stop the processing and move back to the calling class(using startActivityForResult). Instead I now call finish from my onCreate and use a boolean to determine if I could successfully access the directory.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.dialog_load_template);
boolean fileLoadStatus = getTemplates();
if(fileLoadStatus)
{
populateTemplateList(templatePaths);
}
else
{
setResult(RESULT_CANCELED);
finish();
}
}
private boolean getTemplates()
{
boolean fileLoadStatus = false;
//Gets file directory for saved templates
File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");
//Checks if path exists in other word if any templates have been saved before
if(finalMarkTemplateDir.isDirectory())
{
templatePaths = finalMarkTemplateDir.listFiles();
fileLoadStatus = true;
}
else
{
Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();
}
return fileLoadStatus;
}

Categories

Resources