java 'File.mkdirs()' 的结果被忽略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41312244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:48:18  来源:igfitidea点击:

Result of 'File.mkdirs()' is ignored

javaandroid

提问by Jaymin Bhadani

This is my Code inside myDir.mkdirs();this code show me that warning of Result of File.mkdirs()is ignored.

这是我在myDir.mkdirs();此代码中的代码向我显示 Result of 的警告File.mkdirs()被忽略。

I try to fix this Warning but I failed.

我尝试修复此警告,但失败了。

   private void saveGIF() {
            Toast.makeText(getApplicationContext(), "Gif Save", Toast.LENGTH_LONG).show();
            String filepath123 = BuildConfig.VERSION_NAME;
            try {
                File myDir = new File(String.valueOf(Environment.getExternalStorageDirectory().toString()) + "/" + "NewyearGIF");enter code here

    //My Statement Code This Line Show Me that Warning

 myDir.mkdirs();

                File file = new File(myDir, "NewyearGif_" + System.currentTimeMillis() + ".gif");
                filepath123 = file.getPath();
                InputStream is = getResources().openRawResource(this.ivDrawable);
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] img = new byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT];
                while (true) {
                    int current = bis.read();
                    if (current == -1) {
                        break;
                    }
                    baos.write(current);
                }
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            mediaScanIntent.setData(Uri.fromFile(new File(filepath123)));
            sendBroadcast(mediaScanIntent);
        }

回答by Kevin Wallis

The method mkdirshas a booleanreturn value, which you didn't use.

该方法mkdirs有一个boolean您没有使用的返回值。

 boolean wasSuccessful = myDir.mkdirs();

The create operation returns a value, which indicates if the creation of the directory was successful. For example, the result value wasSuccessfulcan be used to display an error when it is false.

create 操作返回一个值,该值指示目录创建是否成功。例如,结果值wasSuccessful可用于在为 false 时显示错误。

if (!wasSuccessful) { 
    System.out.println("was not successful."); 
}

From the Java docsabout the booleanreturn value:

来自关于返回值的Java 文档boolean

true if and only if the directory was created, along with all necessary parent directories; false otherwise

当且仅当创建了目录以及所有必需的父目录时才为真;否则为假

回答by Tobias Otto

The idea behind the return-value of mkdir is, that every IO-Operation could fail and your program should react to this situation.

mkdir 返回值背后的想法是,每个 IO 操作都可能失败,您的程序应该对这种情况做出反应。

回答by Muhammad Hassan

File myDirectory = new File(Environment.getExternalStorageDirectory(),"NewyearGIF");
if(!myDirectory.exists()) {
   myDirectory.mkdirs(); 
}else{
   // Directory already exist
}

回答by riju

File CDir = new File(Environment.getExternalStorageDirectory(), IMPORT_DIRECTORY);
if (!CDir.exists()) {
    boolean mkdir = CDir.mkdir();
    if (!mkdir) {
        Log.e(TAG, "Directory creation failed.");
    }
}

mkdir return a Boolean value. we need to catch the return value from mkdir .Replace your code with this and check (warning of Result of File.mkdirs() is ignored.) will be gone

mkdir 返回一个布尔值。我们需要从 mkdir 捕获返回值。用这个替换你的代码并检查(忽略 File.mkdirs() Result 的警告。)将消失

回答by ClassA

This is a old question but still, the simplest way I've found is:

这是一个老问题,但我发现的最简单的方法是:

File imageThumbsDirectory = getBaseContext.getExternalFilesDir("ThumbTemp");

if(imageThumbsDirectory != null) {
    if (!imageThumbsDirectory.exists()) {
        if (imageThumbsDirectory.mkdir()) ; //directory is created;
    }
}

回答by Chirag Sheta

Just put this code:

只需输入以下代码:

File myDirectory = new File(Environment.getExternalStorageDirectory(),"NewyearGIF");
if(!myDirectory.exists()) {
   myDirectory.mkdirs(); 
}else{
   // Directory already exist
}

If application running in above Lollipop, then you need to add runtime permission for storage.

如果应用程序在上面运行Lollipop,那么您需要为存储添加运行时权限。