對不起,如果這個問題已經被問到, 我在這個問題上搜索了很多,但沒有得到解決方案。PDFDocument文件保存
我正在開發一個android應用程序,它使用PDFDocument的android庫生成PDF文件。
有一些奇怪的問題,在生成PDF文件後,它在電話的文件資源管理器中可見,但在計算機文件資源管理器中不可見(如Mac的Android文件傳輸,Windows的我的電腦和Ubuntu的文件資源管理器)認爲我錯過了代碼中的某些東西。請幫忙。
這裏是我的代碼
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageinfo = new PdfDocument.PageInfo.Builder(PAGEWIDTH,PAGEHEIGHT,1).create();
PdfDocument.Page page = document.startPage(pageinfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setTextSize(getResources().getDimension(R.dimen.textsize));
paint.setTextAlign(Paint.Align.CENTER);
paint.setColor(ContextCompat.getColor(getApplicationContext(),R.color.colorName));
canvas.drawText(getResources().getString(R.string.app_name),PAGEWIDTH/2,50,paint);
document.finishPage(page);
String targetPdf = "/sdcard/documents/myfile.pdf";
File filepath = new File(targetPdf);
try {
filepath.createNewFile();
document.writeTo(new FileOutputStream(filepath));
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.success),
Toast.LENGTH_SHORT).show();
}
catch (IOException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.error) + e.toString(),
Toast.LENGTH_LONG).show();
}
document.close();
手機文件管理器圖片
電腦文件瀏覽器圖片
什麼叫電腦文件瀏覽器是什麼意思?模擬器通過Android設備監視器? –
我的意思是在Mac上的Android文件傳輸和Ubuntu上的文件瀏覽器(當我將手機連接到計算機時,該文件在計算機上不可見) –
不,它不會是這樣,可能是您在將數據傳輸到Mac時出現問題。如果您的文件已創建並且可以在移動設備上打開,這意味着您的代碼工作已經完成並且工作正常,請嘗試正確地複製/傳輸文件 –