【发布时间】:2022-01-03 19:37:50
【问题描述】:
这是我在myDir.mkdirs(); 中的代码,此代码显示File.mkdirs() 的结果警告被忽略。
我尝试修复此警告,但失败了。
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);
}
【问题讨论】:
-
Result of 'File.mkdirs()' is ignored。是的。由你!您没有查看返回值。 -
Docs 说,>你可能会提到文件的更深的路径,但是,不需要创建所有的父目录。因为,有些人可能已经创建了。有些不是。因此,即使它未能创建已经存在的目录,它也会创建不存在的目录,这将产生成功的结果。创建所有必要的目录后,它将给出真实的结果。因此,在这种情况下需要检查布尔条件。