安卓开发地址选择器获取外部储存u盘地址并创建pdf文件并写入数据到pdf文件里

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.provider.DocumentsContract;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.io.IOException;
import java.io.OutputStream;

public class PdfWriteToUsbActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_OPEN_DIRECTORY = 1;
    private String selectedDirectory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_write_to_usb);

        // 打开文档树选择器
        openDirectoryPicker();
    }

    private void openDirectoryPicker() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        startActivityForResult(intent, REQUEST_CODE_OPEN_DIRECTORY);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_OPEN_DIRECTORY && resultCode == Activity.RESULT_OK && data != null) {
            // 获取用户选择的目录的 URI
            selectedDirectory = data.getData().toString();

            // 创建 PDF 文件并写入数据
            createAndWriteToPdf();
        }
    }

    private void createAndWriteToPdf() {
        if (selectedDirectory != null) {
            DocumentFile pickedDir = DocumentFile.fromTreeUri(this, Uri.parse(selectedDirectory));

            if (pickedDir != null && pickedDir.exists() && pickedDir.isDirectory()) {
                // 在选择的目录下创建一个 PDF 文件
                DocumentFile newFile = pickedDir.createFile("application/pdf", "new_file.pdf");

                if (newFile != null && newFile.exists()) {
                    // 写入数据到 PDF 文件
                    writeDataToPdf(newFile);
                } else {
                    // 文件创建失败
                    Toast.makeText(this, "PDF file creation failed", Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            Toast.makeText(this, "Please select a directory first", Toast.LENGTH_SHORT).show();
        }
    }

    private void writeDataToPdf(DocumentFile pdfFile) {
        try {
            // 创建 PdfWriter 和 PdfDocument
            OutputStream outputStream = getContentResolver().openOutputStream(pdfFile.getUri());
            PdfWriter pdfWriter = new PdfWriter(outputStream);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);

            // 创建 Document
            Document document = new Document(pdfDocument);

            // 写入数据到 PDF
            document.add(new Paragraph("Hello, this is a sample PDF document."));

            // 关闭 Document
            document.close();

            // 显示成功消息
            Toast.makeText(this, "PDF file created and data written successfully", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常
        }
    }
}
 

你可能感兴趣的:(android)