-1

I have recently made an app memegenerator screenshot : enter image description here

I want whenever I click on the button to create meme it should automatically save that picture below (which is a fragment) How do I do that I have no idea?

This is what I found on internet to place in my code

Should I add this on my mainactivity file or the fragment java file

` public void createBitmap() { //Log.d(Const.DEBUG,"Creating a Bitmap");

    Bitmap bmp;

    ViewGroup v = (ViewGroup)((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
    v.setDrawingCacheEnabled(true);

    bmp = Bitmap.createBitmap(v.getDrawingCache());

    File directory = new File(Environment.getExternalStorageDirectory()+ File.separator);
    File file = new File(directory,"DankMeme");

    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);

}`

**update This my fragment code **

public class BottomSectionFragment extends Fragment {
private static TextView topMemeText;
private static TextView bottomMemeText;
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.bottom_picture_fragment,container,false);
    topMemeText = (TextView)view.findViewById(R.id.topMemeText);
    bottomMemeText = (TextView)view.findViewById(R.id.bottomMemeText);

    return view;
}
public void setMemeText(String top,String bottom)
{
    topMemeText.setText(top);
    bottomMemeText.setText(bottom);
    //createBitmap();
    // I am calling tackeAndSaveScreenshot func here because when user press button it comes to this func
    // to change text and right after I want screenshot
    tackeAndSaveScreenShot();
}

//Screenshot
public void tackeAndSaveScreenShot() {
    View MainView = getActivity().getWindow().getDecorView();
    MainView.setDrawingCacheEnabled(true);
    MainView.buildDrawingCache();
    Bitmap MainBitmap = MainView.getDrawingCache();
    Rect frame = new Rect();

    getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    //to remove statusBar from the taken sc
    int statusBarHeight = frame.top;
    //using screen size to create bitmap
    int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
    int height = getActivity().getWindowManager().getDefaultDisplay().getHeight();
    Bitmap OutBitmap = Bitmap.createBitmap(MainBitmap, 0, statusBarHeight, width, height - statusBarHeight);
    MainView.destroyDrawingCache();
    try {

        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        //you can also using current time to generate name
        String name="YourName";
        File file = new File(path, name + ".png");
        fOut = new FileOutputStream(file);

        OutBitmap.compress(Bitmap.CompressFormat.PNG, 90, fOut);
        fOut.flush();
        fOut.close();

        //this line will add the saved picture to gallery
        MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Amir Ali
  • 11
  • 7
  • You can do something like this https://stackoverflow.com/questions/45421252/taking-screenshot-of-linearlayout-and-recyclerview/45434407?noredirect=1#comment77846503_45434407 – Farhana Naaz Ansari Aug 02 '17 at 05:26

1 Answers1

1

This method will take screenshot from your mobile screen. you can change the MainView to your view that you want to take screenshot from.

Don't forget to add "Write external storage" permission to manifest

public void tackeAndSaveScreenShot(Activity mActivity) {
    View MainView = mActivity.getWindow().getDecorView();
    MainView.setDrawingCacheEnabled(true);
    MainView.buildDrawingCache();
    Bitmap MainBitmap = MainView.getDrawingCache();
    Rect frame = new Rect();

    mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    //to remove statusBar from the taken sc
    int statusBarHeight = frame.top;
    //using screen size to create bitmap 
    int width = mActivity.getWindowManager().getDefaultDisplay().getWidth();
    int height = mActivity.getWindowManager().getDefaultDisplay().getHeight();
    Bitmap OutBitmap = Bitmap.createBitmap(MainBitmap, 0, statusBarHeight, width, height - statusBarHeight);
    MainView.destroyDrawingCache();
    try {

        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        //you can also using current time to generate name
        String name="YourName";
        File file = new File(path, name + ".png");
        fOut = new FileOutputStream(file);

        OutBitmap.compress(Bitmap.CompressFormat.PNG, 90, fOut);
        fOut.flush();
        fOut.close();

        //this line will add the saved picture to gallery
        MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

calling method by using :

Button memeDanke = (Button) view.findViewById(R.id.memeDanke);
memeDanke.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             tackeAndSaveScreenShot(getActivity());
        }
    });
Behnam Eskandari
  • 1,011
  • 12
  • 27