Exchange

Save Image From ImageView to Gallery in Android Studio - Android Studio Tutorials

 in AndroidManifest.xml


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

in MainActivity.java

on Create Method


ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

image saving method


private void saveImg() {

imgQrCode.setDrawingCacheEnabled(true);
imgQrCode.buildDrawingCache();
imgQrCode.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

Bitmap bitmap = imgQrCode.getDrawingCache();

String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(root + "/Download");
String filename = String.format("%d.png",System.currentTimeMillis());
File myfile = new File(file, filename);

if(myfile.exists()){
myfile.delete();
}

try {
FileOutputStream fileOutputStream = new FileOutputStream(myfile);
bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();

Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();

imgQrCode.setDrawingCacheEnabled(false);

}catch (Exception e){
e.printStackTrace();
Toast.makeText(this, "Error" + e.toString(), Toast.LENGTH_SHORT).show();
}

}
Android Studio Tutorials
Android Studio Tutorials










Post a Comment

0 Comments