【问题标题】:How do I take screenshot or save as image my fragment app in android studio如何在 android studio 中截取屏幕截图或将我的片段应用程序另存为图像
【发布时间】:2017-08-02 14:58:56
【问题描述】:

我最近制作了一个应用程序 memegenerator 截图:enter image description here

我希望每当我点击按钮来创建 meme 它应该自动保存下面的图片(这是一个片段) 我不知道该怎么做?

这是我在互联网上找到的放在我的代码中的内容

我应该将它添加到我的 mainactivity 文件还是片段 java 文件中

` public void createBitmap() { //Log.d(Const.DEBUG,"创建位图");

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

}`

**更新这是我的片段代码**

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

【问题讨论】:

标签: android android-studio android-fragments


【解决方案1】:

此方法将从您的手机屏幕截图。您可以将 MainView 更改为要从中截屏的视图。

不要忘记在清单中添加“写入外部存储”权限

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

使用调用方法:

Button memeDanke = (Button) view.findViewById(R.id.memeDanke);
memeDanke.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             tackeAndSaveScreenShot(getActivity());
        }
    });

【讨论】:

  • 所以我应该复制这段代码并将其放在我的片段 java 文件中,这里唯一要更改的是视图?是吗?
  • 是的。只需将此代码复制到您的 java 类或片段中即可。您可以更改视图并自定义大小。如果您在片段中使用它,请使用 getActivity() 获取所需的参数@AmirAli
  • 使用这个:mActivity.getContentResolver(); @AmirAli
  • 但我已经编辑了它,因为我无法传递任何参数,我想要屏幕截图,当有人按下来涂抹 meme 文本时
  • 你能提供函数调用吗?我应该在 oncreateview 中调用它吗?
猜你喜欢
  • 2013-11-17
  • 2011-03-18
  • 2013-11-26
  • 2014-05-05
  • 2015-12-10
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多