首页 文章

getResourceAsStream返回null,尽管被调用的文件与getResourceAsStream类在同一目录中被调用

提问于
浏览
2

我导入了一个由亚马逊编码的Android样本,涉及我从这里获得的AWS的DynamoDB,大概是为Eclipse编写的:https://github.com/awslabs/aws-sdk-android-samples/tree/master/DynamoDBMapper_UserPreference

由于Android Studio(0.8.1)使用gradle而不是ant,因此在导入时自然会在dir结构方面自动移动,因此(部分)它看起来像这样:

enter image description here

PropertyLoader获取从AwsCredentials.properties连接到数据库DynamoDB所需的TVM凭据信息 . 相关方法:

public class PropertyLoader {

    private boolean hasCredentials = false;
    private String tokenVendingMachineURL = null;
    private boolean useSSL = false;
    private String testTableName = null;

    private static PropertyLoader instance = null;

    public static PropertyLoader getInstance() {
        if ( instance == null ) {
            instance = new PropertyLoader();
        }

        return instance;
    }

    public PropertyLoader() {
        try {
            Properties properties = new Properties();
            properties.load( this.getClass().getResourceAsStream( "AwsCredentials.properties" ) );

            this.tokenVendingMachineURL = properties.getProperty( "tokenVendingMachineURL" );
            this.useSSL = Boolean.parseBoolean( properties.getProperty( "useSSL" ) );
            this.testTableName = properties.getProperty( "testTableName" );

            if ( this.tokenVendingMachineURL == null || this.tokenVendingMachineURL.equals( "" ) || this.tokenVendingMachineURL.equals( "CHANGEME" ) || this.testTableName.equals( "" ) ) {
                this.tokenVendingMachineURL = null;
                this.useSSL = false;
                this.hasCredentials = false;
                this.testTableName = null;
            }
            else {
                this.hasCredentials = true;
            }
        }
        catch ( Exception exception ) {
            Log.e( "PropertyLoader", "Unable to read property file." );
        }
    }

但是getResourceAsStream行 properties.load( this.getClass().getResourceAsStream( "AwsCredentials.properties" ) ); 返回null . 正如您在我的屏幕截图中看到的那样,AwsCredentials.properties与PropertyLoader位于同一个目录中并匹配大小写,根据我对该方法的读数,这应该是所有需要的:http://mindprod.com/jgloss/getresourceasstream.html

getResourceAsStream() is always returning null

我已经尝试了其他的东西,例如前缀""(即 properties.load( this.getClass().getResourceAsStream( "\AwsCredentials.properties" ) ); 并复制凭证文件并放置在src文件夹中(你可以't see it in this screenshot because the explorer sorts by filetype(?) and places '主' first, but it')):

getResourceAsStream returning null

但是,这也没有解决问题 . 尝试过这些选项并完成研究后,我很困惑为什么它会返回null . 我怎样才能解决这个问题?

2 回答

  • 0

    在/ src / main /下创建了一个名为resources的目录,并在那里放置了AwsCredentials.properties并使用了

    properties.load( PropertyLoader.class.getClassLoader().getResourceAsStream( "AwsCredentials.properties" ) );
    

    代替

    properties.load( this.getClass().getResourceAsStream("AwsCredentials.properties" ) );
    

    不像我想的那么优雅,但它有效 .

  • 7

    长达一天我也在努力解决这个问题 . 最后我能够非常巧妙地解决这个问题 . 问题不在于JAVA,而在于所有项目结构 . 例如 . 在Android Studio中,整个项目位于 src/main/java ,而 main 是项目的 flavour . 因此,如果你've file(-s) to read from in source'的包(例如) com/my/example/app ,你必须编辑 build.gradle 文件以便阅读( clazz.getResourceAsStream(file) )才能正常工作 . 即在 android 下定义 sourceSets 如下:

    android {
        /* ... Your stuff ... */
    
        sourceSets {
            // Lets have two flavours to make it more clear
            main {
                resources.srcDirs = ['src/main/java']
            }
    
            flavourFoo {
                resources.srcDirs = ['src/flavourFoo/java']
            }
        }
    }
    

    希望这可以帮助!

相关问题