I'm creating an Android app that manages screenshot folders in the gallery.
One of the functions of the app is to automatically delete screenshot images, and to do this, I created two classes: DeleteActivity
and MyJobService
. MyJobService
class is doing background work with JobScheduler
.So I'm going to send uri
from the DeleteActivity
class to MyJobService
class and delete pictures from the screenshot folder with the uri
, but this is the code I've worked on so far, but there's no error, and nothing happens. What's the problem? Is there a way to automatically delete pictures other than using uri
? Please let me know if there's a good way.
DeleteAcitivity.class
public class MainActivity extends AppCompatActivity {
private static final int READ_REQUEST_CODE = 42;
private static final int JOB_REQUEST_CODE = 43;
public static final String PREFS_NAME = "MyPrefsFile";
private static final int DELETE_JOB_KEY = 123;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("LastSetting", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
final int spinnerSelected = sharedPreferences.getInt("LastClick", 0);
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
final View txt1 = findViewById(R.id.txt1);
final View txt2 = findViewById(R.id.txt2);
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("switchkey", false);
Switch deleteSwitch = findViewById(R.id.switchdel);
deleteSwitch.setChecked(silent);
deleteSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
txt1.setEnabled(true);
spinner.setEnabled(true);
txt2.setEnabled(true);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, JOB_REQUEST_CODE);
} else {
txt1.setEnabled(false);
spinner.setEnabled(false);
txt2.setEnabled(false);
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.cancel(DELETE_JOB_KEY);
Log.d(TAG, "Job cancelled");
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("switchkey", isChecked).commit();
}
});
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.days_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(spinnerSelected);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editor.putInt("LastClick", position).commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
String txtSpinner = spinner.getSelectedItem().toString();
Button picture = (Button) findViewById(R.id.btnfind);
picture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK) {
try {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
}
Bitmap img = getBitmapFromUri(uri);
DocumentsContract.deleteDocument(getContentResolver(), uri);
} // try
catch (Exception e) {
} // catch
} // if
if(requestCode == JOB_REQUEST_CODE)
{
if(resultData != null) {
ComponentName componentName = new ComponentName(MainActivity.this, MyJobService.class);
PersistableBundle bundle = new PersistableBundle();
bundle.putString("imageUri", resultData.getData().toString());
JobInfo.Builder builder = new JobInfo.Builder(DELETE_JOB_KEY, componentName)
.setExtras(bundle)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.setPeriodic(15 * 60 * 1000);
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.schedule(builder.build());
}
}
} // onActivityResult()
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}
}
MyJobService.class
public class MyJobService extends JobService {
private static final String TAG = "MyJobService";
private boolean jobCancelled = false;
private JobParameters jobParameters;
private DeleteAsyncTask deleteAsyncTask;
@Override
public boolean onStartJob(JobParameters params) {
this.jobParameters = params;
PersistableBundle persistableBundle = params.getExtras();
String strUri = persistableBundle.getString("imageUri");
Uri uri = Uri.parse(strUri);
deleteAsyncTask = (DeleteAsyncTask) new DeleteAsyncTask().execute(uri);
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "onStopJob: Job cancelled before completion");
if(null != deleteAsyncTask){
if(!deleteAsyncTask.isCancelled())
deleteAsyncTask.cancel(true);
}
return true;
}
private class DeleteAsyncTask extends AsyncTask<Uri, Integer, String>{
@Override
protected String doInBackground(Uri... uris) {
try {
DocumentsContract.deleteDocument(getContentResolver(), uris[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "Job Finished!";
}
@Override
protected void onProgressUpdate(Integer... values){
super.onProgressUpdate(values);
Log.d(TAG, "onProgressUpdate: i was: " + values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d(TAG, "onPostExecute: message: " + s);
jobFinished(jobParameters, true);
}
}
}