首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略'File.mkdirs()‘的结果

忽略'File.mkdirs()‘的结果
EN

Stack Overflow用户
提问于 2016-12-24 18:00:09
回答 7查看 17.8K关注 0票数 14

这是我在myDir.mkdirs();中的代码这段代码告诉我File.mkdirs()结果的警告被忽略了。

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

代码语言:javascript
复制
   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);
        }
EN

回答 7

Stack Overflow用户

发布于 2016-12-24 18:02:59

方法mkdirs有一个boolean返回值,您没有使用它。

代码语言:javascript
复制
 boolean wasSuccessful = myDir.mkdirs();

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

代码语言:javascript
复制
if (!wasSuccessful) { 
    System.out.println("was not successful."); 
}

在关于boolean返回值的Java docs中:

当且仅当创建了目录以及所有必需的父目录时,

为true;否则为false

票数 19
EN

Stack Overflow用户

发布于 2018-12-11 16:27:41

代码语言:javascript
复制
File CDir = new File(Environment.getExternalStorageDirectory(), IMPORT_DIRECTORY);
if (!CDir.exists()) {
    boolean mkdir = CDir.mkdir();
    if (!mkdir) {
        Log.e(TAG, "Directory creation failed.");
    }
}

mkdir返回布尔值。我们需要捕获来自mkdir的返回值,并使用this和.Replace检查(忽略File.mkdirs()结果的警告)。将会消失

票数 5
EN

Stack Overflow用户

发布于 2016-12-24 18:14:42

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41312244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档