首页 > 编程知识 正文

Android/data,赛斯系列之二

时间:2023-05-05 16:14:59 阅读:119041 作者:2453

【5年安卓零开始播放系列之28】安卓存储(3):assets文件

【代码是最好的老师】

1 .摘要用于存储assets目录app的原始文件assets目录下的所有文件未编译,而是按原样打包到APK中,并使用特定工具类AssetManager在assets目录中2-不压缩图像3-h5混合开发相关文件; 4-音视频文件与res/raw不同,assets下的文件使用R.xx.id浏览方法功能list(stringpath )获取path下的文件和文件夹名称open(stringfilename ) 缺省设置为ACCESS_STREAMING模式open

未指定: ACCESS_UNKNOWN

随机: ACCESS_RANDOM

顺序: ACCESS_STREAMING

缓存:关闭ACCESS_BUFFERclose ()回收资源2 .使用加载文本. txt

加载图像

加载h5

2.1 privatevoidreadassettxt ((字符串结果=' '; try { InputStream is=getAssets ().open ) ' word.txt ); int lenght=is.available (; byte[] buffer=new byte[lenght]; is.read(Buffer ); result=new string (缓冲区,' utf8 '; }catch(exceptione ) { e.printStackTrace ); } MTV.settext (word.txt : (n ' result ); } 2.2图像专用void readassetimg () { Bitmap bitmap=null; try { InputStream is=getAssets ().open ) ' img/IC_test.png ' ); bitmap=bitmap factory.decode stream (is; }catch(exceptione ) { e.printStackTrace ); (if )位图!=null(mimgshow.setimagebitmap ) bitmap; } 2.1 htmlprivatevoidreadasseth5({ string filename=' file ://Android _ asset/index.html '; MTV.settext (文件名称); webviewclientwebviewclient=newwebviewclient (; mweb view.setwebviewclient (webview客户端; mweb view.load URL (文件名称; }3.使用assets存储文件需要直接引用时,请注意尽量不要存储大文件。 4 .也可以使用其他代码activity.java

publicclassmainactivityextendsappcompatactivity { textview MTV; ImageView mImgShow; webview mweb视图; @ overrideprotectedvoidoncreate (bundlesavedinstancestate ) super.oncreate ) savedinstancestate; setcontentview (r.layout.activity _ main; findviewbyid (r.id.BTN _ read _ assets _ txt ).setonclicklistener ) newview.onclicklistener ) @overridepublic }; ); findviewbyid (r.id.BTN _ read _ assets _ img ).setonclicklistener ) newview.onclicklistener ) { @Override p

ublic void onClick(View view) { readAssetImg(); } }); findViewById(R.id.btn_read_assets_h5).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { readAssetH5(); } }); mTv = findViewById(R.id.tv_content); mImgShow= findViewById(R.id.img_show); mWebView= findViewById(R.id.web_view); checkPermission(); } private void readAssetH5() { String fileName = "file:///android_asset/index.html"; mTv.setText(fileName); WebViewClient webViewClient = new WebViewClient(); mWebView.setWebViewClient(webViewClient); mWebView.loadUrl(fileName); } private void readAssetImg() { Bitmap bitmap = null; try { InputStream is = getAssets().open("img/ic_test.png"); bitmap = BitmapFactory.decodeStream(is); } catch (Exception e) { e.printStackTrace(); } if (bitmap != null){ mImgShow.setImageBitmap(bitmap); } } private void readAssetTxt() { String result = ""; try { InputStream is = getAssets().open("word.txt"); int lenght = is.available(); byte[] buffer = new byte[lenght]; is.read(buffer); result = new String(buffer, "utf8"); } catch (Exception e) { e.printStackTrace(); } mTv.setText("word.txt:n"+result); }}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" > <Button android:id="@+id/btn_read_assets_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="read_assets_txt" /> <Button android:id="@+id/btn_read_assets_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="read_assets_img" /> <Button android:id="@+id/btn_read_assets_h5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="read_assets_h5" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容:" /> <ImageView android:id="@+id/img_show" android:layout_width="80dp" android:layout_height="80dp"/> <WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。