Save Part of Activity / Fragment as Image

I am trying to save part of my activity without a toolbar and status bar. The code I have now saves the entire screen. See image below.

enter image description here

The code I have right now is:

llIDCardRootView = (LinearLayout) view.findViewById(R.id.ll_id_card_root_view); llIDCardContainer = (LinearLayout) llIDCardRootView.findViewById(R.id.ll_id_card_view); private void createBitmap() { Log.d(Const.DEBUG, "Creating Bitmap"); Bitmap bmp; //View v = llIDCardContainer.getRootView(); //View v = activity.getWindow().getDecorView().findViewById(android.R.id.content); //View v = activity.findViewById(R.id.ll_id_card_root_view); ViewGroup v = (ViewGroup) ((ViewGroup) activity .findViewById(android.R.id.content)).getChildAt(0); v.setDrawingCacheEnabled(true); // v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), // View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); // v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); // v.buildDrawingCache(true); bmp = Bitmap.createBitmap(v.getDrawingCache()); File directory = new File(Environment.getExternalStorageDirectory() + File.separator); File file = new File(directory, FILE_NAME); try { FileOutputStream out = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } v.destroyDrawingCache(); v.setDrawingCacheEnabled(false); } 

The saved image.

enter image description here

How can I just save the part that I need from the fragment?

+5
source share
3 answers

Use the function below to save any image in an image file. If you need to save the Fragment , call the function below in the fragment.

 public static Bitmap getBitmapFromView(View view) { //Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); //Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); //Get the view background Drawable bgDrawable =view.getBackground(); if (bgDrawable!=null) //has background drawable, then draw it on the canvas bgDrawable.draw(canvas); else //does not have background drawable, then draw white background on the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas view.draw(canvas); //return the bitmap return returnedBitmap; } 
+5
source

Use this

 container_layout.setDrawingCacheEnabled(true); container_layout.buildDrawingCache(true); Bitmap saveBm = Bitmap.createBitmap(container_layout.getDrawingCache()); container_layout.setDrawingCacheEnabled(false); 

Now you can save saveBm as a file

+1
source

you need to make a separate view to save this image, and then you can select a screenshot of this view, for example:

 LinearLayout content = findViewById(R.id.rlid); content.setDrawingCacheEnabled(true); Bitmap bitmap = content.getDrawingCache(); File file,f; if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache"); if(!file.exists()) { file.mkdirs(); } f = new File(file.getAbsolutePath()+file.seperator+ "filename"+".png"); } FileOutputStream ostream = new FileOutputStream(f); bitmap.compress(CompressFormat.PNG, 10, ostream); ostream.close(); } catch (Exception e){ e.printStackTrace(); } 

and don't forget to add permission to manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1
source

Source: https://habr.com/ru/post/1232257/


All Articles