这是我在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);
}发布于 2016-12-24 18:02:59
方法mkdirs有一个boolean返回值,您没有使用它。
boolean wasSuccessful = myDir.mkdirs();create操作返回一个值,该值指示目录创建是否成功。例如,当结果值为false时,可以使用结果值wasSuccessful来显示错误。
if (!wasSuccessful) {
System.out.println("was not successful.");
}在关于boolean返回值的Java docs中:
当且仅当创建了目录以及所有必需的父目录时,
为true;否则为false
发布于 2018-12-11 16:27:41
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()结果的警告)。将会消失
发布于 2016-12-24 18:14:42
mkdir返回值背后的想法是,每个IO操作都可能失败,您的程序应该对这种情况做出反应。
https://stackoverflow.com/questions/41312244
复制相似问题