尝试在内部存储中创建文件夹,但代码仅在oppo手机中工作,而在其他品牌的手机中不起作用



>尝试在内部存储中创建文件夹,但代码仅在oppo手机中工作,而不适用于三星,小米等其他品牌手机

public void createPDF()
{
    TextView dttt = (TextView)findViewById(R.id.dttt);
    String da  = dttt.getText().toString();
    final Cursor cursor = db.getDateWise(da);

    Document doc = new Document();

    try {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CollactionApp"+ "/PDF";
        File dir = new File(path);
        if(!dir.exists())
            dir.mkdirs();
        Log.d("PDFCreator", "PDF Path: " + path);
        int i = 1;
        File file = new File(dir, "Datewise" + da + ".pdf" );
        FileOutputStream fOut = new FileOutputStream(file);
        PdfWriter.getInstance(doc, fOut);
        //open the document
        doc.open();

}

// Check premissions in Manifest and at run time

String root_sd = Environment.getExternalStorageDirectory().toString();
        createExDirectory("CollactionApp", root_sd);
        String path = root_sd + "/CollactionApp"+ "/PDF.pdf";
        File f = new File(path);
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        public static String createExDirectory(String folderName, String pathToExternalStorage) {
        String returnPath = "";
        boolean success = false;
        Boolean isSDPresent = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED);
        if (isSDPresent) {
            // SD-card is present
            File appDirectory = new File(pathToExternalStorage + "/" + folderName);
            if (appDirectory.exists()) {
                success = true;
            } else {
                success = appDirectory.mkdirs();
            }
            if (success) {
                returnPath = appDirectory.getAbsolutePath();
            } else {
            }
        }
        return returnPath;
    }

您使用的路径指向外部存储。我以前有一个应用程序,直到最近,它已经在所有设备上运行了很长时间,所以我不得不对它进行重构,所以我不再使用 getExternalStorageDirectory((。

我现在使用 ContextgetFilesDir(),我没有再次遇到同样的问题。

您可能希望路径如下所示以使用内部存储。

File internalDir = getContext().getFilesDir();
File myFolder = new File(internalDir, "/CollactionApp"+ "/PDF");
String path = myFolder.getAbsolutePath;

相关内容

最新更新