<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>'
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageview"
android:layout_gravity="center"
android:layout_marginTop="@dimen/_35sdp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Image"
android:id="@+id/savegallery"
/>
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button saveimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageview);
saveimage = (Button)findViewById(R.id.savegallery);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
saveimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveToGallery();
}
});
}
private void saveToGallery(){
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
FileOutputStream outputStream = null;
File file = Environment.getExternalStorageDirectory();
File dir = new File(file.getAbsolutePath() + "/MyPics");
dir.mkdirs();
String filename = String.format("%d.png",System.currentTimeMillis());
File outFile = new File(dir,filename);
try{
outputStream = new FileOutputStream(outFile);
}catch (Exception e){
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
try{
outputStream.flush();
}catch (Exception e){
e.printStackTrace();
}
try{
outputStream.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}