2015-04-07 17 views
13

我有这样一段代码Android的API级别<19和“尝试可以使用自动资源管理”警告

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

    } 

} 

产生恼人的警告“可以尝试使用自动资源管理”。该问题是我不能使用Java 7尝试与资源导致我的应用程序目标API级别14(如果我使用它会得到一个错误)。有没有办法抑制这个令人讨厌的警告?

+1

使用SuppressLint –

+0

和我应该使用什么样的ID?我不想使用“全部”.. –

回答

19

@SuppressWarnings( “TryFinallyCanBeTryWithResources”)的伎俩:)

+0

这只是删除警告,但它不会在真实设备上工作<19 – huluyige

+0

所以,有什么建议吗? @huluyige – KaKa

+1

@KaKa不使用java 7的“试用资源”:) – huluyige

0

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

希望它能帮助!