我正在开发一个用于填写pdf表格的应用,我将pdf保存到内部存储,然后使用actION_CREATE_DOCUMENT进行共享。此意图返回一个URI,然后我将本地pdf复制到其中。
所有这些都很好,但是意图打开了一个文件浏览器弹出窗口,以便用户可以选择将pdf保存在哪里,这就是当用户按下SAVE时的问题所在。该应用程序在该位置创建了0b文件(应如此),但随后重新打开文件资源管理器,提示用户再次保存,这种情况发生了2到3次,然后最终真正关闭,此时pdf数据覆盖了众多0b文件中的最新文件。
public int WRITE_REQUEST_CODE=45;
...
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void buttonExport(View view) {
Toast.makeText(this,"Exporting - This will take around 1min",Toast.LENGTH_LONG).show();
// Template of PDF with acrofields (template.pdf).
// TRY to open the pdf stored in the raw res directory
// then convert it to a file object by copying it
try {
//
InputStream inputStream =null;
if (template.equals("crfminortemplate")){
inputStream = getResources().openRawResource(R.raw.crfminorpdftemplat);
}
else if (template.equals("crfmajortemplate")){
inputStream = getResources().openRawResource(R.raw.crfmajorpdftemplat);
}
File tempFile = new File(getFilesDir(),template);
//
copyFile(inputStream,new FileOutputStream(tempFile));
// Now Questions res is tempFile ..
} catch (IOException e) {
throw new RuntimeException("Can't create temp file ",e);
}
try {
PDfmanipulation.fillPDF(view,template,fileName,Answers);
} catch (IOException e) {
e.printStackTrace();
}
catch (NullPointerException e)
{
e.printStackTrace();
}
Toast.makeText(this,"Export Complete - save to drive or email",Toast.LENGTH_LONG).show();
sharePDF(getFilesDir()+"/"+fileName+".pdf");
}
private void sharePDF(String PDFPPath) {
Uri path = FileProvider.geturiForFile(this,"com.example.cst_app_v3",new File(getFilesDir(),fileName+".pdf"));
Intent shareIntent = new Intent();
shareIntent.setaction(Intent.actION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Sent from CST App Android " + PDFPPath);
shareIntent.putExtra(Intent.EXTRA_STREAM,path);
shareIntent.addflags(Intent.flaG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("application/pdf");
startactivityForResult(shareIntent,0);
Intent saveIntent = new Intent();
saveIntent.setaction(Intent.actION_CREATE_DOCUMENT);
saveIntent.putExtra(Intent.EXTRA_TITLE,fileName+".pdf");
saveIntent.addCategory(Intent.CATEGORY_OPENABLE);
saveIntent.addflags(Intent.flaG_GRANT_READ_URI_PERMISSION);
saveIntent.addflags(Intent.flaG_GRANT_WRITE_URI_PERMISSION);
saveIntent.setType("application/pdf");
startactivityForResult(saveIntent,WRITE_REQUEST_CODE);
Intent chooserIntent = Intent.createChooser(shareIntent,"Share or Save ...");
Intent[]arrayofintent = {saveIntent};
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,arrayofintent);
startactivityForResult(chooserIntent,0);
startactivity(chooserIntent);
Log.d("Alert",path.getauthority()+" "+ path.getPath());
}
@Override
protected void onactivityResult(int requestCode,int resultCode,@Nullable Intent data) {
if (resultCode == RESULT_OK && requestCode == WRITE_REQUEST_CODE) {
//DO THE COPY PASTE FROM LOCAL to THE URI data
}
}
再次
我希望用户启动保存意图并让文件选择器弹出,让用户命名文件并选择保存位置,按保存,然后返回到我的我的应用程序,将pdf复制到他们的位置指定
如果有人知道/曾经遇到过这个问题,或者认为他们知道可能会发生什么,那么很高兴收到您的来信。