rafi0101 / Android-Room-Database-Backup

Simple tool to backup and restore your room database in Android
MIT License
239 stars 19 forks source link

How to Stop from delete backup file after uninstall app #9

Closed rushilYiion closed 2 years ago

rushilYiion commented 3 years ago

Hello, is there any way to stop from delete backup files after uninstall the app

rafi0101 commented 3 years ago

Hi,

it is not so easy to implement this. For this I initial implemented "Save your backup to external app storage" there you can view the backups via the filesytem and save them somewhere else. A few months ago I started with this feature to save backups everywhere and read them from everywhere via a dialog where you can choose you destination / source. But that turned out to be not so easy and I am currently very busy with other topics. If you need it very urgently, maybe I can find a weekend in the near future to have a look at it.

rushilYiion commented 3 years ago

Thanks for your help 👍🏻 If i need your help on this topic then i will email you.

On Thu, 26 Aug, 2021, 6:21 pm Raphael Ebner, @.***> wrote:

Hi,

it is not so easy to implement this. For this I initial implemented "Save your backup to external app storage" there you can view the backups via the filesytem and save them somewhere else. A few months ago I started with this feature to save backups everywhere and read them from everywhere via a dialog where you can choose you destination / source. But that turned out to be not so easy and I am currently very busy with other topics. If you need it very urgently, maybe I can find a weekend in the near future to have a look at it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rafi0101/Android-Room-Database-Backup/issues/9#issuecomment-906379629, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOCXNHA5I7YUYWURBS2YP6DT6Y2EFANCNFSM5CASG3PA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

ghost commented 2 years ago

Hi @rafi0101 . This seems to be a great work but it can't be full advantage of it if the uninstall destroys all backups. Then I fully quote the @rushilYiion request.

I've implemented a method to create a file (API 21+) in an environment directory and I'm glad to share with you. This method requires a FileProvider. I hope this will help your to develop more quickly.

public static class FileData{
    public final @NonNull Uri uri;
    public final @NonNull OutputStream os;

    public FileData(@NonNull Uri uri, @NonNull OutputStream os){
        this.uri = uri;
        this.os = os;
    }
}

/**
 * This method creates a file in the external storage.
 * @param context A valid context.
 * @param environmentDirectory A valid environment directory.
 * @param folder A folder name.
 * @param fileName The file name.
 * @param inputStream The input stream.
 * @param mimeType the file mime type.
 * @return The generated file uri or null if an error has occurred.
 *
 * @see Environment
 */
public static @Nullable FileData createFile(@NonNull Context context,
    @NonNull final String environmentDirectory,
    @Nullable String folder,
    @NonNull String fileName,
    @Nullable InputStream inputStream,
    @NonNull String mimeType){
    if(MimeTypeMap.getSingleton().hasMimeType(mimeType)){
        try {
            OutputStream fos;
            Uri fileUri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                String path = environmentDirectory;
                if(!TextUtils.isEmpty(folder)) path = path + File.separator + folder;
                ContentResolver resolver = context.getContentResolver();
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, path);
                fileUri = resolver.insert(getContentUri(environmentDirectory), contentValues);
                if(fileUri == null){
                    Timber.e("Error while retrieving file uri %s", path);
                    return null;
                }
                fos = resolver.openOutputStream(fileUri);
            }
            else {
                File dir = !TextUtils.isEmpty(folder)
                ? new File(Environment.getExternalStoragePublicDirectory(environmentDirectory).toString() + File.separator + folder)
                : new File(Environment.getExternalStoragePublicDirectory(environmentDirectory).toString());
                String path = dir.exists() ? dir.getPath() : dir.mkdirs() ? dir.getPath() : null;
                if(TextUtils.isEmpty(path)){
                    Timber.e("Error while creating new folder %s", dir.toString());
                    return null;
                }
                File file = new File(path, fileName + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType));
                fileUri = FileProvider.getUriForFile(context,context.getApplicationContext().getPackageName() + ".provider", file);
                fos = new FileOutputStream(file);
            }
            if(inputStream != null){ //write content to file if available
                byte[] buf = new byte[1024];
                int length;
                while ((length = inputStream.read(buf)) > 0) {
                    fos.write(buf, 0, length);
                }
            }
            return new FileData(fileUri, fos);
        }
        catch (IOException e) {
            Timber.e(e, "Error while writing content into file");
            return null;
        }
    }
    else {
        Timber.e("Unhandled mime type %s", mimeType);
        return null;
    }
}
rafi0101 commented 2 years ago

@thezukkino thank you for your work! 👍🏻 I'll have a look at it in the next few days

ghost commented 2 years ago

@rafi0101 , do you have news about the task? Do you need any kind of help?

rafi0101 commented 2 years ago

@thezukkino, I'm currently working on it. Little bit try and error and finding the best way to implement this feature into the library. Do you prefer using a documet tree picker or passing a custom file location by code or both?

ghost commented 2 years ago

Imho the best way is to pass custom file location. A document tree picker is a UX content. Really thanks for your help.

rafi0101 commented 2 years ago

I created a new release, see: v1.0.0-beta08 I think this will close this issue.