Android: issue创建目录



我试图在画廊文件夹中创建一个目录(存储视频和照片,仅此而已),我尝试了以下代码:

 File dir = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/dirname");
 dir.makedirs() ;

问题是当我使用文件管理器浏览SD卡文件和文件夹时,一切正常,文件确实存在,但是当我打开我的画廊时,没有名为dirname的目录。有什么问题吗?

谢谢:-)

这是我的函数,取位图并将其保存在目录中…

 public void saveBitmap(Bitmap bitmap) {
    if (createDirIfNotExists("TestApp")) {
        String filePath = Environment.getExternalStorageDirectory()
                + File.separator + "TestApp/TestSC" + date_value + "_" + time_value + ".png";
        File imagePath = new File(filePath);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            hideLoadingDialog();
            showAlert(getString(R.string.successfullySavedPic));
        } catch (FileNotFoundException e) {
            hideLoadingDialog();
            showAlert(getString(R.string.problemOccured));
            Log.e("error", e.getMessage(), e);
        } catch (IOException e) {
            hideLoadingDialog();
            showAlert(getString(R.string.problemOccured));
            Log.e("error", e.getMessage(), e);
        }
    } else {
        String filePath = Environment.getExternalStorageDirectory()
                + File.separator + "Pictures/TestSC" + date_value + "_" + time_value + ".png";
        File imagePath = new File(filePath);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            hideLoadingDialog();
            showAlert(getString(R.string.successfullySavedPic));
        } catch (FileNotFoundException e) {
            hideLoadingDialog();
            showAlert(getString(R.string.successfullySavedPic));
            Log.e("error", e.getMessage(), e);
        } catch (IOException e) {
            hideLoadingDialog();
            showAlert(getString(R.string.successfullySavedPic));
            Log.e("error", e.getMessage(), e);
        }
    }

}

createDirIfNotExist ():

public static boolean createDirIfNotExists(String path) {
    boolean ret = true;
    File file = new File(Environment.getExternalStorageDirectory(), path);
    if (!file.exists()) {
        if (!file.mkdirs()) {
            Log.e("TravellerLog :: ", "Problem creating Image folder");
            ret = false;
        }
    }
    return ret;
}

Try with getAbsolutePath():

String filePathDir = Environment.getExternalStorageDirectory()
            .getAbsolutePath()
            + "/"
            + appNameFolder
            + "/"
            + innerFolder;
File fileDir = new File(filePathDir);
if (!fileDir.exists())
    fileDir.mkdirs();

最新更新