How to save the layout view as image or pdf to sd card in android?

后端 未结 2 1356
一整个雨季
一整个雨季 2020-12-03 06:02

In my application, I have a view with customer details, I want to save that view as image or PDF to SD card then print the view through third party application (Otherwise pr

相关标签:
2条回答
  • 2020-12-03 06:47

    Above code work properly but, image override because of same name so, for avoid this problem add below line:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
    
    Date now = new Date();
    
    String fileName = formatter.format(now);
    
    f = new File(file.getAbsolutePath()+file.separator+ "image_"+fileName+".png");
    
    0 讨论(0)
  • 2020-12-03 06:49

    Add permission in the manifest file

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

    Use the code below

    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();
    }
    
    0 讨论(0)
提交回复
热议问题