文件并调用时.exists(),但无法找到它后存在到它、存在、文件、exists

2023-09-04 23:06:38 作者:只想当个好男人@

我试图创建一个文件,该文件最终将持有图片(EXTRA_OUTPUT)是这样的:

 文件mediaStorageDir =新的文件(Environment.getExternalStoragePublicDirectory(
                      Environment.DIRECTORY_PICTURES),scouthouse);
            如果(!mediaStorageDir.exists()){
                mediaStorageDir.mkdirs();
            }

            SimpleDateFormat的S =新的SimpleDateFormat(ddMMyyyyhhmmss);
            字符串格式= s.format(新日期());
            档案文件=新的文件(mediaStorageDir.getPath()+文件分割符+格式+.JPG);
            尝试 {
                file.createNewFile();
            }赶上(IOException异常E){
                e.printStackTrace();
            }
 

在mediaStorageDir.exists()返回true,但我找不到在Windows文件夹。该位置是sdcard0 /图片/ scouthouse /。我有一个图片/文件夹,但Android不创建/ scouthouse /文件夹。 当我尝试去$ C C文件$用Bitmapfactory返回null。

EDIT!

所以,我已经简化的方法有点趴下去根,基本上有这样的:

 私人无效createDir(){
    档案文件=新的文件(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES),scouthouse);
    如果(!file.mkdirs()){
        Log.d(文件,文件没有创建);
    }
}
 

这将创建一个文件目录,日志未记录的第一次。这样的目录已被创建,但 我找不到它在手机上的文件系统的任何地方。

 私人文件createNewFile(){
    档案文件=新的文件(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES),scouthouse+文件分割符+test.jpg放在);
    尝试 {
        如果(file.createNewFile()){
            Log.d(文件,yisS基因);
            返回的文件;
        }
    }赶上(IOException异常E){
        e.printStackTrace();
    }

    返回null;
}
 
wordpress漏洞 Wordpress File manager 任意文件上传漏洞复现

该方法返回null。该文件不能创建。

解决方案   

在mediaStorageDir.exists()返回true,但我找不到在Windows文件夹。

大概是因为 MediaStore 不知道该目录,因此MTP连接到Windows不知道该目录。您可以尝试使用 MediaScannerConnection SCANFILE()来解决这个问题,虽然我只尝试过的文件,而不是目录。此外,即使索引后,你可能需要从你的资源管理器窗口做刷新,或断开/重新连接设备,因为Windows可以缓存中期计划的结果和未检测到变化,即使机器人也知道。

I'm trying to create a file that is eventually going to hold a picture (EXTRA_OUTPUT) like this:

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                      Environment.DIRECTORY_PICTURES), "scouthouse");
            if(!mediaStorageDir.exists()) {
                mediaStorageDir.mkdirs();
            }

            SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
            String format = s.format(new Date());
            File file = new File(mediaStorageDir.getPath() + File.separator + format + ".jpg");
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

The mediaStorageDir.exists() returns true, but I can't find the folder in windows. The location is sdcard0/Pictures/scouthouse/. I do have a Pictures/ folder but Android doesn't create the /scouthouse/ folder. When I try to decode the file with a Bitmapfactory it returns null.

EDIT!

So I've simplified the method a bit to get down to the root and basically have this:

   private void createDir() {
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "scouthouse");
    if(!file.mkdirs()) {
        Log.d("file", "file not created");
    }   
}

This creates a file dir, and the Log isn't logged for the first time. So the directory has been created, however I can't find it on the phones file system anywhere.

    private File createNewFile() {
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "scouthouse" + File.separator + "test.jpg");
    try {
        if(file.createNewFile()) {
            Log.d("file", "yiss");
            return file;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

This method returns null. The file can't be created..

解决方案

The mediaStorageDir.exists() returns true, but I can't find the folder in windows.

Probably that is because MediaStore does not know about the directory, and so the MTP connection to Windows does not know about the directory. You can try using MediaScannerConnection and scanFile() to resolve this, though I have only tried that for files, not directories. Also, even after indexing, you may need to do "Refresh" from your Explorer window, or disconnect/reconnect the device, as Windows may cache the MTP results and not detect a change even if Android knows about it.