java.io.FileNotFoundException on Itextpdf Library tutorial

萝らか妹 提交于 2019-12-25 09:59:23

问题


I have been trying iTextpdf today ang got into some shoddy error. mainly: java.io.FileNotFoundException: /storage/emulated/0/Download/pdfdemo20170521_145348.pdf: open failed: EACCES (Permission denied)

I have already implemented the proper permissions like

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

but it also does not work. here is the code for my file processing:

public class SelfNoteFragment extends Fragment {
private View mRootView;
private EditText mSubjectEditText, mBodyEditText;
private Button mSaveButton;

public SelfNoteFragment() {
    // Required empty public constructor
}

public static SelfNoteFragment newInstance(){
    SelfNoteFragment fragment = new SelfNoteFragment();
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mRootView = inflater.inflate(R.layout.fragment_self_note, container, false);
    mSubjectEditText = (EditText) mRootView.findViewById(R.id.edit_text_subject);
    mBodyEditText = (EditText) mRootView.findViewById(R.id.edit_text_body);
    mSaveButton = (Button) mRootView.findViewById(R.id.button_save);

    mSaveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mSubjectEditText.getText().toString().isEmpty()){
                mSubjectEditText.setError("Subject is empty");
                mSubjectEditText.requestFocus();
                return;
            }

            if (mBodyEditText.getText().toString().isEmpty()){
                mBodyEditText.setError("Body is empty");
                mBodyEditText.requestFocus();
                return;
            }

            try {
                createPdf();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "FILE", Toast.LENGTH_SHORT).show();
            } catch (DocumentException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "Document", Toast.LENGTH_SHORT).show();
            }
        }
    });
    return mRootView;
}


private void createPdf() throws FileNotFoundException, DocumentException {

    File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS), "pdfdemo");
    if (!pdfFolder.exists()) {
        pdfFolder.mkdir();
        Log.i("TAG", "Pdf Directory created");
    }

    //Create time stamp
    Date date = new Date() ;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);

    //Filename
    File myFile = new File(pdfFolder + timeStamp + ".pdf");

    OutputStream output = new FileOutputStream(myFile);

    //Designate the size
    Rectangle pagesize = new Rectangle(216f, 720f);
    Document document = new Document(pagesize, 36f, 72f, 108f, 180f);

    PdfWriter.getInstance(document, output);

    //OPEN ITEXT FOR DOCUMENT SCANNING
    document.open();

    document.add(new Paragraph(mSubjectEditText.getText().toString()));
    document.add(new Paragraph(mBodyEditText.getText().toString()));

    //If scanning is done, Close the document
    document.close();

}

when i look at my logcat, an error on the line 107 appears. which is the line of: OutputStream output = new FileOutputStream(myFile);


回答1:


For sdk>=23 you need to ask Runtime permission.

Here is a list of Runtime permission required in Android Os.

in your Oncreate()

Ask for Permission using:

if (Build.VERSION.SDK_INT >= 23) {
    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
       //you code..
       //read_file()
    } else {
        //request permission
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}
else { 

    //permission is automatically granted on sdk<23 upon installation
    //Your code
    //read_file()
}

Catch the permission result in:

    @Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {

        case 0: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                //read_file()
            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
 }


来源:https://stackoverflow.com/questions/44094083/java-io-filenotfoundexception-on-itextpdf-library-tutorial

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!