且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何保存布局视图图像或PDF格式的SD卡中的android?

更新时间:2023-09-28 10:59:58

添加权限清单文件

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

使用以下

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