简体   繁体   中英

File Couldn't Save On All real device Android Studio

//Save PDF file

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


                String filePath = Environment.getExternalStorageDirectory() +"/"+  "Allotment" +timeStamp+ ".pdf";

                File myFile = new File(filePath);

                try {
                    OutputStream output = new FileOutputStream(myFile);
                    pdfDocument.writeTo(output);
                    output.flush();
                    output.close();
                    Toast.makeText(AllotmentDoc.this, "PDF Created Succesfully", Toast.LENGTH_LONG).show();
                    Toast.makeText(AllotmentDoc.this, "PDF Saved On Memory", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }

I want to save my pdf file that's why I wrote some line of code. But it works on some device and doesnt work on some other device. What is the problem. how can i solve it.

I think you are facing this problem in some deviece because of

Environment.getExternalStorageDirectory()

You can use

Environment.getExternalStorageDirectory().getAbsolutePath();

Have in mind though, that getExternalStorageDirectory() is not going to work properly on some phones eg Galaxy Tab 2, Motorola razr maxx, as it has 2 cards /mnt/sdcard and /mnt/sdcard-ext - for internal and external SD cards respectfully. You will be getting the /mnt/sdcard only reply every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM