Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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中pdf文件的屏幕截图转换_Android - Fatal编程技术网

android中pdf文件的屏幕截图转换

android中pdf文件的屏幕截图转换,android,Android,我正在尝试将活动转换为pdf格式。我已拍摄了与位图相同的屏幕截图。请帮助我将该屏幕截图转换为所需的pdf文件。谢谢。 请提供代码。谢谢。请尝试以下问题: 或者在项目中包含itextpdf-5.3.2.jar这是我使用的代码 1) 主要活动 公共类MainActivity扩展了活动{ //按钮btn_getpdf private LinearLayout linearLayout; private Bitmap myBitmap; @Override

我正在尝试将活动转换为pdf格式。我已拍摄了与位图相同的屏幕截图。请帮助我将该屏幕截图转换为所需的pdf文件。谢谢。
请提供代码。谢谢。

请尝试以下问题:


或者在项目中包含itextpdf-5.3.2.jar这是我使用的代码

1) 主要活动

公共类MainActivity扩展了活动{ //按钮btn_getpdf

      private LinearLayout linearLayout;
      private Bitmap myBitmap;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          // btn_getpdf = (Button) findViewById(R.id.button_pdf);
          linearLayout = (LinearLayout) findViewById(R.id.linear1);
          linearLayout.post(new Runnable() {
              public void run() {

                  //take screenshot
                  myBitmap = captureScreen(linearLayout);

                  Toast.makeText(getApplicationContext(), "Screenshot captured..!", Toast.LENGTH_LONG).show();

                  try {
                      if (myBitmap != null) {
                          //save image to SD card
                          saveImage(myBitmap);
                      }
                      Toast.makeText(getApplicationContext(), "Screenshot saved..!", Toast.LENGTH_LONG).show();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

              }
          });

      }

      public Bitmap captureScreen(View v) {

          Bitmap screenshot = null;
          try {

              if (v != null) {

                  screenshot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                  Canvas canvas = new Canvas(screenshot);
                  v.draw(canvas);
              }

          } catch (Exception e) {
              // Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
          }

          return screenshot;
      }

      public void saveImage(Bitmap bitmap) throws IOException {

          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
          File f = new File(root.getAbsolutePath() + "/DCIM/Camera/bitmap.jpg");
          f.createNewFile();
          FileOutputStream fo = new FileOutputStream(f);
          fo.write(bytes.toByteArray());
          fo.close();
      }
  }
2) WritePDF活动

公共类WritePdfActivity扩展了活动 { 私有静态字符串FILE=“DCIM/Camera/GenPdf.pdf”


}

Duplicate可能的副本显示“无法解析符号根”。,即im设置路径的位置。文件f=新文件(root.getAbsolutePath()+“/DCIM/Camera/bitmap.jpg”);我是否将屏幕截图和位图的代码放在同一个类或其他?共享代码…….我添加了doidtext..可以吗?是的,可以@SwagatamDuttaFile f=new File(root.getAbsolutePath()+“/DCIM/Camera/bitmap.jpg”);它显示为无法解析符号根..这里的根是什么?!
      private LinearLayout linearLayout;
      private Bitmap myBitmap;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          // btn_getpdf = (Button) findViewById(R.id.button_pdf);
          linearLayout = (LinearLayout) findViewById(R.id.linear1);
          linearLayout.post(new Runnable() {
              public void run() {

                  //take screenshot
                  myBitmap = captureScreen(linearLayout);

                  Toast.makeText(getApplicationContext(), "Screenshot captured..!", Toast.LENGTH_LONG).show();

                  try {
                      if (myBitmap != null) {
                          //save image to SD card
                          saveImage(myBitmap);
                      }
                      Toast.makeText(getApplicationContext(), "Screenshot saved..!", Toast.LENGTH_LONG).show();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

              }
          });

      }

      public Bitmap captureScreen(View v) {

          Bitmap screenshot = null;
          try {

              if (v != null) {

                  screenshot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                  Canvas canvas = new Canvas(screenshot);
                  v.draw(canvas);
              }

          } catch (Exception e) {
              // Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
          }

          return screenshot;
      }

      public void saveImage(Bitmap bitmap) throws IOException {

          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
          File f = new File(root.getAbsolutePath() + "/DCIM/Camera/bitmap.jpg");
          f.createNewFile();
          FileOutputStream fo = new FileOutputStream(f);
          fo.write(bytes.toByteArray());
          fo.close();
      }
  }
static Image image;
static ImageView img;
Bitmap bmp;
static Bitmap bt;
static byte[] bArray;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    img=(ImageView)findViewById(R.id.imageView1);

    try
    {
        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();

        addImage(document);
        document.close();
    }

    catch (Exception e)
    {
        e.printStackTrace();
    }

}
private static void addImage(DocumentsContract.Document document)
{

    try
    {
        image = Image.getInstance(bArray);  ///Here i set byte array..you can do bitmap to byte array and set in image...
    }
    catch (BadElementException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (MalformedURLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // image.scaleAbsolute(150f, 150f);
    try
    {
        document.add(image);
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}