Use of Assets in Android Studio

 Android Studio adds the Assets directory:

1、In Android Studio, right-click on the  folder and navigate to the Assets Folder.

On the next screen just click Finish.

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

2. You can configure it in the build.gradle file and add the following code

" sourceSets { main { assets.srcDirs = ['src/assets', 'src/assets/'] } } "

android {
compileSdkVersion 24
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.city.linkage"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { assets.srcDirs = ['src/assets', 'src/assets/'] } }
}

Place it according to the path assets.srcDirs=['src/assets'], (note: the path can be changed) as shown in the figure:

<iframe id="iframe_0.7414613890904245" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 172px; height: 155px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2017/0222/180354_n60s_2945455.png?_=6430366%22%20style=%22border:none;max-width:730px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7414613890904245',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>

 

How to get the absolute path of the image in android assert:

the first method:

String path = "file:///android_asset/filename";

The second method:

InputStream abpath = getClass().getResourceAsStream("/assets/文件名");

To convert to String type

String path = new String(InputStreamToByte(abpath ));

private byte[] InputStreamToByte(InputStream is) throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    }

        The path obtained in this way cannot directly access the resources of assets. If an mp3 is placed under assets, the path obtained by the above two methods cannot directly play the mp3 file in it, because it is stored in the assets directory in Android applications. The resource file represents the native resources that the application cannot directly access. The application reads the resources in the form of binary streams through the AssetManager.

        The first method of obtaining the path, as far as I know, is only used for the display of html. For example, if you use webview, then the path of html can be displayed in this way, but not in other cases.

        So what should we do if we want to use MediaPlayer to play the video or music in it? Playback can be done in the following ways:

   

         MediaPlayer mediaPlayer=new MediaPlayer();
            AssetFileDescriptor afd = getAssets().openFd("musics/SleepOceation.mp3");
            mediaPlayer.setDataSource(afd.getFileDescriptor());
            mediaPlayer.prepare(); (note that this sentence must be there!)
            mediaPlayer.start();

 

  OK, so you can play the music or video files under assets smoothly!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326269986&siteId=291194637