Android API 级别 < 19 和 "try can use automatic resource management" 警告
Android API level < 19 and "try can use automatic resource management" warning
我有这段代码
private void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
}finally {
if(inChannel != null) {
inChannel.close();
}
outChannel.close();
}
}
生成烦人的警告 "try can use automatic resource management"。问题是我无法使用 Java 7 try-with-resources 导致我的应用程序目标 api 级别 14(如果我使用它会出错)。有没有办法抑制那个烦人的警告?
@SuppressWarnings("TryFinallyCanBeTryWithResources") 成功了:)
@SuppressLint("NewApi")
也行 =)
例如:
@SuppressLint("NewApi")
public static File saveImage(Bitmap bitmap) {
File file = null;
if (isExternalStorageWritable()) {
file = new File(getFolderStorageDirectory(), getFileNameImage());
try (FileOutputStream out = new FileOutputStream(file)) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
e.getMessage();
}
}
return file;
}
希望对您有所帮助!
我有这段代码
private void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
}finally {
if(inChannel != null) {
inChannel.close();
}
outChannel.close();
}
}
生成烦人的警告 "try can use automatic resource management"。问题是我无法使用 Java 7 try-with-resources 导致我的应用程序目标 api 级别 14(如果我使用它会出错)。有没有办法抑制那个烦人的警告?
@SuppressWarnings("TryFinallyCanBeTryWithResources") 成功了:)
@SuppressLint("NewApi")
也行 =)
例如:
@SuppressLint("NewApi")
public static File saveImage(Bitmap bitmap) {
File file = null;
if (isExternalStorageWritable()) {
file = new File(getFolderStorageDirectory(), getFileNameImage());
try (FileOutputStream out = new FileOutputStream(file)) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
e.getMessage();
}
}
return file;
}
希望对您有所帮助!