getResourceAsStream returning null despite called file being in same dir as class getResourceAsStream is called in

前端 未结 2 2003
轻奢々
轻奢々 2020-12-20 03:21

I imported an Android sample coded by Amazon involving AWS\'s DynamoDB which I got from here and was presumably written for Eclipse: https://github.com/awslabs/aws-sdk-andro

相关标签:
2条回答
  • 2020-12-20 04:05

    For up to a day I was struggling with this as well. And finally I was able to resolve this very neatly. The problem is not in the JAVA but in the all project structure. E.g. in Android Studio the whole project is under src/main/java whereas main is a flavour of the project. So if you've file(-s) to read from in source's package (e.g.) com/my/example/app you have to edit the build.gradle file for read (clazz.getResourceAsStream(file)) to work properly. I.e. under android define sourceSets like this:

    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']
            }
        }
    }
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-20 04:06

    Created a dir called resources under /src/main/ and placed AwsCredentials.properties there and used

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

    instead of

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

    Not as elegant as I would like, but it works.

    0 讨论(0)
提交回复
热议问题