First,download the image and save to your private folder like so:
File file = new File(getExternalFilesDir(), "file.jpg");
Next, grab the content:// uri of the just downloaded image file like so:
Uri sourceUri = resolver.insert( imageCollections, values);
Then, grab the content:// uri of the destination file in the Download folder of
external storage like so:
Uri destinationUri = data.getData();
Create InputStream from the sourceUri like so:
FileInputStream fis = ( FileInputStream ) this.getContentResolver().openInputStream(
sourceUri);
Create OutputStream from the destinationUri like so:
FileOutputStream fos = ( FileOutputStream )
this.getContentResolver().openOutputStream( destination );
copy file from source to destination .
All together:
//after image file is successfully downloaded...
String path = fullPathToRecentlyDownloadedImage;
path should be
like:/storage/emulated/0/Android/data/yourAppPackageName/files/ImageFolder/imageName.png
Dont hard code it.
Uri imageCollections = MediaStore.Image.Media.getContentUri( "external" );
ContentValues values = new ContentValues();
values.put( MediaStore.Image.Media.DISPLAY_NAME, nameOfImage );
values.put( MediaStore.Image.Media.MIME_TYPE, imageMimeType );
values.put( MediaStore.Image.Media.DATE_ADDED, dateAdded );
values.put( MediaStore.Image.Media.DATA, path );
Uri sourceUri= resolver.insert( imageCollections , values);
Then, grab the content:// uri of the destination file in the Download folder of
external storage
Intent i = new Intent();
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
i.setAction( Intent.ACTION_CREATE_DOCUMENT );
i.addCategory( Intent.CATEGORY_OPENABLE );
i.setType( imageMimeType );
i.putExtra( Intent.EXTRA_TITLE, nameOfImage );
startActivityForResult( i, 1000 );
}
The content:// uri will be pushed to onActivityResult() function of MainActivity.Override this function like so:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Super.onActivityResult(requestCode, resultCode, data);
if ( requestCode == 1000 && data != null ){
destinationUri = data.getData();
We now have the destination and source content:// uris, initializing Input and Output streams from these uris as well as copying bytes from source to destination is up to you.
Note:The intent code above will open a save as activity, navigate to the Download folder and click save.
Hope will help
getExternalFilesDir()
or the data directory of my app.