0

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;
}
  • 1
    This is a mess. Please put all code in the blocks. Further we dont know what your code should do, which problems you have and there is/are no question,(s). – blackapps Sep 15 '22 at 19:33
  • I just want to move files without manage external storage permisison in android – Jivanlal Bhavsar Sep 18 '22 at 17:59
  • We are waiting until you put all code in blocks. And tell what you want in your post. Not in a comment. – blackapps Sep 18 '22 at 18:40

0 Answers0