加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 忽略’File.mkdirs()’的结果

发布时间:2020-12-15 00:08:30 所属栏目:Java 来源:网络整理
导读:这是myDir.mkdirs()中的我的代码;此代码显示我忽略了File.mkdirs()的Result警告. 我尝试修复此警告,但我失败了. private void saveGIF() { Toast.makeText(getApplicationContext(),"Gif Save",Toast.LENGTH_LONG).show(); String filepath123 = BuildConfig
这是myDir.mkdirs()中的我的代码;此代码显示我忽略了File.mkdirs()的Result警告.

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

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);
        }

解决方法

方法mkdirs有一个布尔返回值,你没有使用它.
boolean wasSuccessful = myDir.mkdirs();

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

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

从Java docs左右的布尔返回值:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读