Save Text as image into Internal Storage on Button on Click in android studio . Today we are saving text as a image into phone internal storage in this we are not taking permission .if you added permission then use this code
Step 1: Call into MainActivity.java where you call on Button Click
Bitmap bitmap = takeScreenshot(myViewholder.relativeLayout);
Uri uri = null;
uri = Saveimageintostorage(bitmap, context, "Satus");
if (uri != null) {
Toast.makeText(context, "Download Completed " +
uri.toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Something went worng try again
", Toast.LENGTH_SHORT).show();
}
Step 2 : paste these two these two methods
public Bitmap takeScreenshot(RelativeLayout relativeLayout) {
View rootView = relativeLayout;
rootView.setDrawingCacheEnabled(true);
rootView.buildDrawingCache(true);
Bitmap b1 =
Bitmap.createBitmap(rootView.getDrawingCache(true));
rootView.setDrawingCacheEnabled(false); // clear
drawing cache
return b1;
}
public Uri Saveimageintostorage(Bitmap bmp, Context context,
String name) {
Uri bmpUri = null;
try {
File folder = new
File(Environment.getExternalStorageDirectory() + "/" +
context.getResources().getString(R.string.app_name));
boolean isfoldercreated = false;
if (!folder.exists()) {
isfoldercreated =
folder.mkdir();
}
String csvfinename =
name.toString() + System.currentTimeMillis() + ".jpeg";
String filepathname =
folder.getAbsolutePath() + "/" + csvfinename;
FileOutputStream out = new
FileOutputStream(filepathname);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
out.flush();
bmpUri =
Uri.parse(String.valueOf(filepathname));
MediaScannerConnection.scanFile(context,
new
String[]{filepathname}, null,
new
MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch (IOException e) {
Toast.makeText(context,
e.getMessage().toLowerCase(), Toast.LENGTH_SHORT);
e.printStackTrace();
}
return bmpUri;
}
Comments
Post a Comment