Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

PDF Creator does not save on the device

I wrote an application that creates pdf from the entered content but there is a problem, I can't find the error. The application does not ask me for permission to write and thus does not save the pdf in the device memory where in my code there may be an error when saving android studio does not show an error

 public class karta extends AppCompatActivity {

     private static final int STORAGE_CODE = 1000;
     EditText nazwa;
     EditText ulica;
     EditText kod;
     EditText miasto;
     EditText numer;
     EditText wizyta;
     EditText ph;
     EditText nip;
     EditText pln;
     EditText kat;
     EditText cennik;
     Button btnCreatePdf;

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

         nazwa = findViewById(R.id.nazwa);
         ulica = findViewById(R.id.ulica);
         kod = findViewById(R.id.kod);
         miasto = findViewById(R.id.miasto);
         numer = findViewById(R.id.numer);
         wizyta = findViewById(R.id.wizyta);
         ph = findViewById(R.id.ph);
         nip = findViewById(R.id.nip);
         pln = findViewById(R.id.pln);
         kat = findViewById(R.id.kat);
         cennik = findViewById(R.id.cennik);
         btnCreatePdf = findViewById(R.id.savepdf);

         // deklaracja pola
         if (TextUtils.isEmpty("")) {
             nazwa.setError("Pole wymagane");
             ulica.setError("Pole wymagane");
             kod.setError("Pole wymagane");
             miasto.setError("Pole wymagane");
             numer.setError("Pole wymagane");
             wizyta.setError("Pole wymagane");
             ph.setError("Pole wymagane");
             nip.setError("Pole wymagane");
             pln.setError("Pole wymagane");
             kat.setError("Pole wymagane");
             cennik.setError("Pole wymagane");
             return;
         }

         btnCreatePdf.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
                     if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                         PackageManager.PERMISSION_DENIED) {
                         String[] permissions = {
                             Manifest.permission.WRITE_EXTERNAL_STORAGE
                         };
                         requestPermissions(permissions, STORAGE_CODE);
                     } else {
                         savePdf();
                     }

                 } else {
                     savePdf();
                 }
             }

         });
     }

     private void savePdf() {
         Document mDoc = new Document();
         String mFileName = new SimpleDateFormat("yyyMMdd_HHmmss",
             Locale.getDefault()).format(String.valueOf(System.currentTimeMillis()));
         String mFilePath = Environment.getExternalStorageDirectory() + "/" + mFileName + ".pdf";

         try {
             PdfWriter.getInstance(mDoc, new FileOutputStream(mFilePath));
             mDoc.open();
             String mText = nazwa.getText().toString();
             String mText1 = ulica.getText().toString();
             String mText2 = kod.getText().toString();
             String mText3 = miasto.getText().toString();
             String mText4 = numer.getText().toString();
             String mText5 = wizyta.getText().toString();
             String mText6 = ph.getText().toString();
             String mText7 = nip.getText().toString();
             String mText8 = pln.getText().toString();
             String mText9 = kat.getText().toString();
             String mText0 = cennik.getText().toString();

             mDoc.addAuthor(mText6);
             mDoc.addTitle("Nowy klient" + mText);
             mDoc.add(new Paragraph(mText));

             mDoc.close();

             Toast.makeText(this, mFileName + ".pdf
 zapisane w
 " + mFilePath, Toast.LENGTH_SHORT).show();

         } catch (Exception e) {
             Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
         }
     }

     @Override
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
         switch (requestCode) {
             case STORAGE_CODE:
                 {
                     if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                         savePdf();
                     } else {
                         Toast.makeText(this, "Permission denied...!", Toast.LENGTH_SHORT).show();
                     }
                 }
         }
     }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...