Save Image From Imageview To Device Gallery
I'm trying to save an image from ImageView to devices gallery. I tried this code Code Edit: URL url = new URL(getIntent().getStringExtra('imageURL')); File f = new File(ur
Solution 1:
Simple:
use this code:
//to get the image from the ImageView (say iv)BitmapDrawabledraw= (BitmapDrawable) iv.getDrawable();
Bitmapbitmap= draw.getBitmap();
FileOutputStreamoutStream=null;
FilesdCard= Environment.getExternalStorageDirectory();
Filedir=newFile(sdCard.getAbsolutePath() + "/YourFolderName");
dir.mkdirs();
StringfileName= String.format("%d.jpg", System.currentTimeMillis());
FileoutFile=newFile(dir, fileName);
outStream = newFileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Additionally, in order to refresh the gallery and to view the image there:
Intentintent=newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
Also make sure that your app has the storage permission enabled:
Go to Device Settings>Device>Applications>Application Manager>"your app">Permissions>Enable Storage permission!
Manifest permissions:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" />
Solution 2:
ImageViewiv= (ImageView)findViewById(R.id.your_image_view);
Then set your image and when you want to retrieve/save it
iv.buildDrawingCache();
Bitmapbmp= iv.getDrawingCache();
Then save as normal to gallery
FilestorageLoc= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //context.getExternalFilesDir(null);Filefile=newFile(storageLoc, filename + ".jpg");
try{
FileOutputStreamfos=newFileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
scanFile(context, Uri.fromFile(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
privatestaticvoidscanFile(Context context, Uri imageUri){
IntentscanIntent=newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(imageUri);
context.sendBroadcast(scanIntent);
}
and of course make sure your manifest has permissions to write to external storage.
Solution 3:
U can easily get a file from URL
Filef=newFile(url.getPath());
Post a Comment for "Save Image From Imageview To Device Gallery"