Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将部分活动/片段另存为图像_Android_Android Fragments_Screenshot - Fatal编程技术网

Android 将部分活动/片段另存为图像

Android 将部分活动/片段另存为图像,android,android-fragments,screenshot,Android,Android Fragments,Screenshot,我正在尝试保存部分活动,但没有工具栏和状态栏。我现在拥有的代码保存了整个屏幕。请参考下图 我现在拥有的代码: llIDCardRootView = (LinearLayout) view.findViewById(R.id.ll_id_card_root_view); llIDCardContainer = (LinearLayout) llIDCardRootView.findViewById(R.id.ll_id_card_view); private void c

我正在尝试保存部分活动,但没有工具栏和状态栏。我现在拥有的代码保存了整个屏幕。请参考下图

我现在拥有的代码:

   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);
    }
正在保存的图像

如何从片段中保存所需的部分?

使用此选项

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

您现在可以将
saveBm
另存为文件

您必须为另存为图像创建一个单独的视图,然后您可以选择该视图的屏幕截图,如:

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();
}
不要忘了在
清单中添加
权限

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

使用下面的功能将任何视图保存到图像文件。如果需要保存在
片段中
,请在片段中调用下面的函数

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's 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;
    }

裁剪图像并保存,这样会更好