Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rushilYiion opened this issue Aug 12, 2021 · 8 comments · Fixed by #10
Closed

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

rushilYiion opened this issue Aug 12, 2021 · 8 comments · Fixed by #10
Assignees

Comments

@rushilYiion
Copy link

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

@rafi0101
Copy link
Owner

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
Copy link
Author

rushilYiion commented Aug 26, 2021 via email

@ghost
Copy link

ghost commented Oct 28, 2021

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
Copy link
Owner

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

@rafi0101 rafi0101 self-assigned this Nov 1, 2021
@ghost
Copy link

ghost commented Nov 10, 2021

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

@rafi0101
Copy link
Owner

@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?

rafi0101 added a commit that referenced this issue Nov 14, 2021
@ghost
Copy link

ghost commented Nov 15, 2021

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

@rafi0101 rafi0101 linked a pull request Nov 15, 2021 that will close this issue
@rafi0101
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants