Android: can not access files created by my application after reinstall my application

Viewed 256

I have application that create files in shared storage, i use MediaStore to create and read files, my application success create and read files, but after i uninstall and reinstall again my application can not access the files created before, even though i check using file manager the files still exist. so, how to access the files created before uninstalled after i reinstall again? Thank you

this is my code to create files

String db_name = c.getString(R.string.db_name);
String backupDbName = "backup-"+utility.getCurrentDateTimeNoSpace()+"-"+db_name;
File currentDB = c.getDatabasePath(db_name);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst;
ParcelFileDescriptor pfd = null;

ContentResolver resolver = c.getContentResolver();
Uri uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DISPLAY_NAME, backupDbName);
values.put(MediaStore.Files.FileColumns.TITLE, backupDbName);
values.put(MediaStore.Files.FileColumns.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS + File.separator
                    + c.getString(R.string.app_name)+ "-" + BACKUP + File.separator + DATABASE);
Uri fileUri = resolver.insert(uri, values);
pfd = resolver.openFileDescriptor(fileUri, "w", null);
dst = new FileOutputStream(pfd.getFileDescriptor()).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
if(pfd != null) pfd.close();

this is code for read the files

@RequiresApi(api = Build.VERSION_CODES.Q)
private Cursor getFiles(String folderName){
    String[] projection = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DISPLAY_NAME};

    String selection = MediaStore.Files.FileColumns.RELATIVE_PATH+" = ? OR "
            + MediaStore.Files.FileColumns.RELATIVE_PATH +" = ?";

    String rel_path = Environment.DIRECTORY_DOCUMENTS + File.separator
            + c.getString(R.string.app_name)+ "-" + BACKUP + File.separator + folderName;

    ContentResolver resolver = c.getContentResolver();
    Uri uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);

    return resolver.query(uri, projection, selection,
            new String[]{rel_path + File.separator, rel_path}, null);
}
0 Answers
Related