if (!file.delete()) {
Uri fileuri = getImageContentUri(Pager2Activity.this, file);
if (fileuri != null) {
DeleteXI.getInstance().with(Pager2Activity.this).delete(launcher, fileuri, pager2Adapter);
myFile = file;
moveImage(fileuri);
IntentSender intentSender = moveFileDirect();
if (intentSender != null) {
try {
startIntentSenderForResult(intentSender, 786, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
public IntentSender moveFileDirect() {
images.clear();
grantedUris.clear();
contentResolver = getContentResolver();
IntentSender result = null;
PendingIntent pendingIntent = null;
File dirSrc = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MIUI/Gallery/Testing App");
if (!dirSrc.isDirectory()) {
if (dirSrc.listFiles() == null) {
Toast.makeText(this, "No Images to move", Toast.LENGTH_SHORT).show();
return null;
}
}
File[] files = dirSrc.listFiles();
for (int i = 0; i < files.length; i++) {
images.add(getImageContentUri(Pager2Activity.this, files[i]));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
pendingIntent = MediaStore.createWriteRequest(contentResolver, images);
result = pendingIntent.getIntentSender();
}
return result;
}
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 786) {
for (int i = 0; i < images.size(); i++) {
if (checkUriPermission(images.get(i), Binder.getCallingPid(), Binder
.getCallingUid(),
Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != PackageManager
.PERMISSION_GRANTED) {
grantedUris.add(images.get(i));
}
}
File destination = new File(Environment.getExternalStorageDirectory(), "MIUI/Gallery/Test1");
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.RELATIVE_PATH, destination.getPath() + "/");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
for (Uri image : grantedUris) {
contentResolver.update(image, values, null);
}
}
Toast.makeText(this, "result code is " + resultCode + " ---- -1 ---- it means RESULT_OK", Toast.LENGTH_SHORT).show();
}
}
public static Uri getImageContentUri(Context context, File imageFile) {
try {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
@SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
cursor.close();
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
}
} catch (Exception e) {
return null;
}
return null;
}