我应该把"assets"文件夹放在 Android Studio 中的什么位置?

I am confused about the assets folder. It doesn't come auto-created in Android Studio, and almost all the forums in which this is discussed talk about Eclipse.

How can the Assets directory be configured in Android Studio?

转载于:https://stackoverflow.com/questions/18302603/where-do-i-place-the-assets-folder-in-android-studio

csdnceshi74
7*4 Check this tut gadgetsaint.com/android/custom-fonts-textview-edittext-android/…
3 年多之前 回复

19个回答

Since Android Studio uses the new Gradle-based build system, you should be putting assets/ inside of the source sets (e.g., src/main/assets/).

In a typical Android Studio project, you will have an app/ module, with a main/ sourceset (app/src/main/ off of the project root), and so your primary assets would go in app/src/main/assets/. However:

  • If you need assets specific to a build, such as debug versus release, you can create sourcesets for those roles (e.g,. app/src/release/assets/)

  • Your product flavors can also have sourcesets with assets (e.g., app/src/googleplay/assets/)

  • Your instrumentation tests can have an androidTest sourceset with custom assets (e.g., app/src/androidTest/assets/), though be sure to ask the InstrumentationRegistry for getContext(), not getTargetContext(), to access those assets

Also, a quick reminder: assets are read-only at runtime. Use internal storage, external storage, or the Storage Access Framework for read/write content.

csdnceshi63
elliott.david please see this and if you can help thanks stackoverflow.com/questions/51927562/…
大约 2 年之前 回复
csdnceshi50
三生石@ There is a src folder in any standard Android Studio project, including those created in Android Studio 3.1.2 and the current canary release of 3.2.
2 年多之前 回复
csdnceshi69
YaoRaoLov On newer versions of Android Studio, there is no more src folder, see below answer for Android Studio to create it for you automatically.
2 年多之前 回复
weixin_41568110
七度&光 Yes, I agree. However, running under a completely default Android Studio 2.2.2, you don't get the exact File Structure on the Project navigator. That's what I intended to point out.
接近 4 年之前 回复
csdnceshi50
三生石@ No, this answer is still accurate, as I am referring to the filesystem (in Android Studio, you get a filesystem view through the Project rendition of the explorer tree).
接近 4 年之前 回复
weixin_41568110
七度&光 This answer is no longer exactly accurate, as the way Android Studio renders the file structure has changed.
接近 4 年之前 回复
csdnceshi65
larry*wei thanks
大约 4 年之前 回复
csdnceshi50
三生石@ Then you do not have countries.txt in the assets/ directory. Here is a sample project that, among other things, copies a file from assets into internal storage. If you have further concerns, please ask a separate Stack Overflow question, where you provide a Minimal, Complete, and Verifiable example.
大约 4 年之前 回复
csdnceshi65
larry*wei how to read a file once assets folder is created . InputStream stream = getApplicationContext().getResources().getAssets().open("countries.txt"); It gives me filenotfound excepton
大约 4 年之前 回复
csdnceshi64
游.程 Thanks for this answer, please can you check this question if you can help, : stackoverflow.com/q/33027188/2595059. Thanks
接近 5 年之前 回复
weixin_41568196
撒拉嘿哟木头 I was about go crazy, thanks.
接近 5 年之前 回复
csdnceshi80
胖鸭 Jeez. The android documentation needs to be way more explicit about this kind of stuff. This was unnecessarily hard to find.
大约 5 年之前 回复
csdnceshi71
Memor.の Thank you! I've been searching for hours for this. I tried creating a sub directory for assets under /src/main/res/ and the directory was being removed from the apk build.
5 年多之前 回复
csdnceshi62
csdnceshi62 Cool. That worked like a charm. I wish there was a way to include Assets font (custom) into Styles.xml rather than using it through code. (My wish might sound too dumb to handle, I am a beginner so excuse me if it is :-P)
大约 7 年之前 回复
csdnceshi50
三生石@ "or do i need to assign the font explicitly by code to the control to see it?" -- oh, absolutely. You will need to call setTypeface() on all TextView widgets (and others that inherit from TextView) that you want to use this particular font.
大约 7 年之前 回复
csdnceshi62
csdnceshi62 I am trying to include a custom font in the assets directory. Should I be good to go by just pasting the .ttf file as src/main/assets/font.ttf ? or do i need to assign the font explicitly by code to the control to see it?
大约 7 年之前 回复

Let Android Studio do it for you.

  1. In Android Studio (1.0 & above), right-click on the enter image description here folder and navigate to the Assets Folder.

enter image description here

  1. On the next screen just click Finish.

enter image description here

And voila! It will create the assets folder in the main target source set.

enter image description here

csdnceshi64
游.程 What would be the link to this folder in a java file?
3 年多之前 回复
csdnceshi58
Didn"t forge Once you add the assets folder as shown above (on v2.1.2), it should be visible inside app > src > main
大约 4 年之前 回复
csdnceshi63
elliott.david Problem: on newer versions of Android Studio (I'm using 2.1.2), empty asset folders are not displayed in the Project Files window, making it difficult to add files. :(
大约 4 年之前 回复
csdnceshi61
derek5. Graphical illustration makes all the difference in the world!
接近 5 年之前 回复
csdnceshi74
7*4 this solution is for Android Studio 1.0 and above. Other answers are obsolete.
5 年多之前 回复

Looking inside the .iml file of your project you will see the following line:

 <option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />

This means the "assets" folder is already declared for Gradle. You will need to create it under src/main/ (I'm using Android Studio 0.4.2).

csdnceshi68
local-host Allways in .im at root of you src.. not in the project directory.. Thats just for gradle stuff.
接近 5 年之前 回复
csdnceshi71
Memor.の check app.iml not <Project-Name>.iml
大约 5 年之前 回复
csdnceshi80
胖鸭 you probably have 2 .iml files, check both of them.
6 年多之前 回复
csdnceshi74
7*4 My .iml doesn't have this, what portion of the file do I add this to?
6 年多之前 回复

In android studio you can specify where the source, res, assets folders are located. for each module/app in the build.gradle file you can add something like:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    sourceSets {
        main {
            java.srcDirs = ['src']
            assets.srcDirs = ['assets']
            res.srcDirs = ['res']
            manifest.srcFile 'AndroidManifest.xml'
        }
    }
}
weixin_41568110
七度&光 is this necessary if one follows the convention?
接近 5 年之前 回复

File > New > folder > assets Folder

Assets Folder

Two ways 1. select app/main folder, Right click and select New => Folder => Asset Folder It will create 'assets' directory in main

  1. Select main folder, Right click and select New => Directory Enter name as 'assets' = > ok

enter image description here

Simple follow this

File > New > folder > assets Folder

Note : App must be selected before creating folder.

need configure parameter for gradle
i hope is will work

// file: build.gradle  
sourceSets {
    main {
        assets.srcDirs = ['src/main/res/icon/', 'src/main/assets/']
    }
}

Either create a directory under /app/src/main or use studio File-> New -> Folder - > Assets Folder.

enter image description hereHi it very simple, first of all, assets folder will not create automatic with the project. we need to create it.

The location of Assets folder is:- App->src->assets

please have a look of two simple images:enter image description here

Note: for craeting assets folder just click on project right click select NEW then folder then Assets, it will create assets folder.

共19条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问