Last active
September 16, 2023 06:22
-
-
Save antocara/5fb2904df2c7de34ebe9 to your computer and use it in GitHub Desktop.
Convert View to Bitmap Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void savedBitmapFromViewToFile(){ | |
//inflate layout | |
View layout = LayoutInflater.from(this).inflate(R.layout.activity_main, null, false); | |
RelativeLayout mContImage = (RelativeLayout) layout.findViewById(R.id.image_cont); | |
//reference View with image | |
mContImage.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); | |
Bitmap bitmap = Bitmap.createBitmap(mContImage.getMeasuredWidth(), mContImage.getMeasuredHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
mContImage.layout(0, 0, mContImage.getMeasuredWidth(), mContImage.getMeasuredHeight()); | |
mContImage.draw(canvas); | |
//save to File | |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); | |
String nameFile = "imageFromView.jpg"; | |
File f = new File(Environment.getExternalStorageDirectory() + File.separator + nameFile); | |
FileOutputStream fo = null; | |
try { | |
fo = new FileOutputStream(f); | |
fo.write(bytes.toByteArray()); | |
fo.close(); | |
} catch (Exception e) { | |
Log.d("Error File:" , "" + e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks