Skip to content

Instantly share code, notes, and snippets.

@varundwarkani
Created May 8, 2020 18:11
Show Gist options
  • Save varundwarkani/401cb237959283755d8f91ff69d0b55a to your computer and use it in GitHub Desktop.
Save varundwarkani/401cb237959283755d8f91ff69d0b55a to your computer and use it in GitHub Desktop.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 12 && resultCode == RESULT_OK && data != null) {
Uri fileUri = data.getData();
try {
assert fileUri != null;
InputStream inputStream = getContentResolver().openInputStream(fileUri);
if (validFile(fileUri)) {
restoreDatabase(inputStream);
} else {
Utils.showSnackbar(findViewById(android.R.id.content), getString(R.string.restore_failed), 1);
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@KaanRF
Copy link

KaanRF commented May 31, 2020

Hello ,

Thanks for the share such a good solutions.

I have a question. I also implemented the file chooser like you and onActivityResult.

here you can see .

    private void showFileChooser() {
        String dbMimeType = "*/*";
        Intent intent = new Intent().setAction( Intent.ACTION_GET_CONTENT );

        @SuppressWarnings("deprecation") String startPath = Environment.getExternalStorageDirectory().getPath();
        Uri uri = Uri.parse( startPath );

        intent.setDataAndType( uri, dbMimeType );

        startActivityForResult( Intent.createChooser( intent,
                getString( R.string.fragment_settings_backup_and_restore_select_database_to_import ) ),
                PICK_FILE_RESULT_CODE );
    }
    @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult( requestCode, resultCode, data );
        if (requestCode == PICK_FILE_RESULT_CODE && resultCode == RESULT_OK) {
            Uri fileUri = null; //The uri with the location of the file
            if (data != null) {
                fileUri = data.getData();
            }

            if (fileUri != null) {
                try {
                    InputStream inputStream = requireContext().getContentResolver().openInputStream( fileUri );
                    if (validFile( fileUri )) {
                        importDatabase( inputStream );
                    } else {
                        Toast.makeText( getActivity(),
                                getString( R.string.fragment_settings_backup_and_restore_restore_failed ),
                                Toast.LENGTH_SHORT ).show();
                    }

                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

My problem is on the Input stream when I select the file from chooser.
When input stream is create with the fileUri path is always null. That is why I can not restore my backed up database.

inputstream;
image

fileUri;

image

Could you please help me ? By the way I did not used your backup method I have another one. But nearly the same.

@KaanRF
Copy link

KaanRF commented May 31, 2020

What makes the getFilePath(context, 0) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment