Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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 将.jpg转换为pdf_Android - Fatal编程技术网

Android 将.jpg转换为pdf

Android 将.jpg转换为pdf,android,Android,在点击应用程序时,我已经通过相机点击了图像并存储在SD卡中。现在我想将其转换为pdf格式,我不知道如何操作。有人能帮我吗。 我想点击一个按钮就可以完成。有可能吗?这里有一个代码可以完成这项工作。我试着评论最多的行数,但是如果你不明白,请告诉我 class JpgToPdfActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.

在点击应用程序时,我已经通过相机点击了图像并存储在SD卡中。现在我想将其转换为pdf格式,我不知道如何操作。有人能帮我吗。
我想点击一个按钮就可以完成。有可能吗?

这里有一个代码可以完成这项工作。我试着评论最多的行数,但是如果你不明白,请告诉我

class JpgToPdfActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jpg_to_pdf_activity);

        // Get button
        Button convertButton = (Button) findViewById(R.id.convert_button);
        convertButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick()
            {
                // Will run the conversion in another thread to avoid the UI to be frozen
                Thread t = new Thread() {
                    public void run()
                    {
                        // Input file
                        String inputPath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";

                        // Output file
                        String outputPath = Environment.getExternalStorageDirectory() + File.separator + "out.pdf";

                        // Run conversion
                        final boolean result = JpgToPdfActivity.this.convertToPdf(inputPath, outputPath);

                        // Notify the UI
                        runOnUiThread(new Runnable() {
                            public void run()
                            {
                                if (result) Toast.makeText(JpgToPdfActivity.this, "The JPG was successfully converted to PDF.", Toast.LENGTH_SHORT).show();
                                else Toast.makeText(JpgToPdfActivity.this, "An error occured while converting the JPG to PDF.", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                };
                t.start();
            }
        });
    }

    public static void convertToPdf(String jpgFilePath, String outputPdfPath)
    {
        try
        {
            // Check if Jpg file exists or not
            File inputFile = new File(jpgFilePath);
            if (!inputFile.exists()) throw new Exception("File '" + jpgFilePath + "' doesn't exist.");

            // Create output file if needed
            File outputFile = new File(outputPdfPath);
            if (!outputFile.exists()) outputFile.createNewFile();

            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(outputFile));
            document.open();
            Image image = Image.getInstance(jpgFilePath);
            document.add(image);               
            document.close();

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

        return false;
    }
}

可能重复:我尝试过使用这种方法,但无法实现。问题是什么?12-09 11:43:49.737:i/System.out25126:Exception java.io.FileNotFoundException:/YourPDFHere.pdf:open失败:EROFS只读文件系统稍等。我正在努力。谢谢你的代码。成功了@poojagupta如果代码有效,那么请标记问题已解决,并向选民投票支持他的方案effort@manitoba我面临的一个问题是,它不是将整个图像转换为pdf格式,而只是其中的一部分。你能帮我吗?我想你必须调整图片的大小。请看这一页