cuốn sách gpt4 ai đã làm

mở pdf bằng tùy chọn openwith android

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-03 00:20:43 27 4
mua khóa gpt4 Nike

Tôi có một bản pdf mà tôi mở bằng ứng dụng của riêng mình. Bây giờ, khi người dùng nhấp vào nút, tôi hiển thị tùy chọn openwith của ứng dụng/pdf. Bây giờ người dùng chọn lựa chọn của mình (ví dụ: adobe reader) và tệp pdf đã mở phải được hiển thị theo lựa chọn của người dùng (trong trường hợp này là adobe reader). Đối với tệp PDF đã mở, tôi có mảng byte và luồng đầu vào.

câu trả lời hay nhất

thử cái này

Tệp tệp = Tệp mới (Môi trường.getExternalStorageDirectory().getAbsolutePath() +"/"+ tên tệp);
Mục tiêu ý định = Ý định mới(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Ý định ý định = Intent.createChooser(đích, "Mở tệp");
thử {
startActivity(ý định);
} bắt (ActivityNotFoundException e) {
// Hướng dẫn người dùng cài đặt phần mềm đọc PDF tại đây, hay gì đó
}

编辑 1

 OutputStream out = new FileOutputStream("out.pdf");
out.write(bArray);
out.close();

Sau khi tạo pdf,

Tệp tệp = Tệp mới ("filepath");
Mục tiêu ý định = Ý định mới(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Ý định ý định = Intent.createChooser(đích, "Mở tệp");
thử {
startActivity(ý định);
} bắt (ActivityNotFoundException e) {
// Hướng dẫn người dùng cài đặt phần mềm đọc PDF tại đây, hay gì đó
}

Chỉnh sửa 2

Tệp myFile = Tệp mới("out.pdf");
OutputStream out = new FileOutputStream(myFile);
out.write(bytArray);
out.close();

Mục tiêu ý định = Ý định mới(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(myFile),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Ý định ý định = Intent.createChooser(đích, "Mở tệp");
thử {
startActivity(ý định);
} bắt (ActivityNotFoundException e) {
// Hướng dẫn người dùng cài đặt phần mềm đọc PDF tại đây, hay gì đó
}

điều này có thể giúp bạn

Chỉnh sửa 3

Mã bên dưới là mã tôi đã tự kiểm tra và hoạt động tốt

Tạo tập tin pdf:

Tệp giải quyếtMeSDCard = Tệp mới ("/sdcard/download/media/output.pdf");

khoảng trống công khai createPDF()
{
byte[] byte = byte mới[]{1,2,3,4,5};

Tệp mediaDir = Tệp mới ("/sdcard/download/media");
if (!mediaDir.exists()){
mediaDir.mkdir();
}

FileOutputStream fos;
thử {

//Tệp ResolveMeSDCard = Tệp mới("/sdcard/download/media/output.pdf");
giải quyếtMeSDCard.createNewFile();
fos = FileOutputStream mới(resolveMeSDCard);
fos.write(byte);
fos.close();
System.out.println("Tệp của bạn đã được ghi");
} catch (IOException e) {
// TODO Khối bắt được tạo tự động
e.printStackTrace();
System.out.println("Tập tin của bạn chưa được ghi");
}

}

Mở tệp pdf:

  khoảng trống công khai openPDF() 
{
Mục tiêu ý định = Ý định mới(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(resolveMeSDCard),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Ý định ý định = Intent.createChooser(đích, "Mở tệp");
thử {
startActivity(ý định);
} bắt (ActivityNotFoundException e) {
// Hướng dẫn người dùng cài đặt phần mềm đọc PDF tại đây, hay gì đó
}
}

bảng kê khai.xml

Thêm các quyền sau


Để ý:
1. Thay đổi thứ tự mã nếu cần.

2. Gọi createPDF(), sau đó gọi OpenPDF().

Đây là mã làm việc.

Về android - mở pdf bằng tùy chọn openwith android, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/38178349/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress