2017-08-02 61 views
-1

我最近開發的應用memegenerator 截圖:enter image description here如何採取截圖保存爲圖像我的片段的應用程序在Android Studio中

我想只要我點擊按鈕來創建米姆 它會自動保存下面的圖片(這是一個片段) 我該怎麼做,我不知道?

這是我在互聯網上找到我的代碼放置

我應該補充這對我mainactivity文件或片段的java文件

'公共無效createBitmap(){ // 日誌。 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(); 
    } 
} 
+0

你可以做這樣的事情https://stackoverflow.com/問題/ 45421252/taken-screenshot-of-linearlayout-and-recyclerview/45434407?noredirect = 1#comment77846503_45434407 – farhana

回答

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

所以我應該複製這段代碼並將它放在我的片段java文件中,唯一需要改變的地方是視圖?是嗎? –

+0

是的。只需將此代碼複製到您的java類或片段中即可。您可以更改視圖並自定義尺寸。如果您在片段中使用getActivity()獲取所需的參數@AmirAli –

+0

無法解析「getContentResolver()」方法 –

相關問題