Android import java.nio.file.Files; cannot be resolved

本小妞迷上赌 提交于 2019-12-27 11:06:17

问题


I am trying out the new Gmail API and the samples use the classes in the java.nio.file package, e.i. Files and FileSystems.

These classes was introduced in Java jdk 1.7 for the record, and since I am running jdk 1.7.0_65 in my Android app I have no idea why Android Studio cannot find these classes.

The imports are:

import java.nio.file.FileSystems;
import java.nio.file.Files;

My build.gradle file of course tells the system to use v. 1.7 like this

android {
    compileSdkVersion 19
    buildToolsVersion '20'
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

I am pointing to the right directory of the jdk:

The jdk is listed in the External Libraries section:

And if I browse through the Java files I can even find java.nio.file.Files and .FileSystems:

Now, what the **** is going on!? From my understanding I am doing everything right here, any suggestions?


回答1:


Android does not offer all classes that "conventional java" has to offer. Files is one of the classes, that Android doesn't offer.

You can have a look at the classes available in Android here: http://developer.android.com/reference/classes.html

So unfortunately you have to use other functions / classes to implement the same functionality.

PS: The class is shown in your screenshot because you browse the classes of java installed on your PC, not those that are be available on the Android phone / tablet.

Update

The Files/FileSystem classes have become available starting with API version 26.




回答2:


Apparently java.nio.file is coming with Android O. https://developer.android.com/reference/java/nio/file/package-summary.html




回答3:


Have a Look of this Link Android support some classes of NIO packages not full classes. But if you need to fast-up your code you can also use apache-commons api for the same.



来源:https://stackoverflow.com/questions/24869323/android-import-java-nio-file-files-cannot-be-resolved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!