歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中使用assets下的資源——圖片資源

Android中使用assets下的資源——圖片資源

日期:2017/3/1 10:22:05   编辑:Linux編程

在Android 應用中使用assets目錄下存放的資源文件,assets目錄下存放的資源代表應用無法直接訪問的原生資源,應用程序通過AssetManager以二進制流的形式來讀取資源。此應用是查看/assets/目錄下的圖片查看器(圖片格式為:.png),在assets目錄下放幾張PNG格式的圖片

該程序的界面十分簡單,只包含一個ImageView和一個按鈕

代碼如下:

布局文件如下:bitmaptest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:gravity="center_vertical"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content">
  8. <Button
  9. android:id="@+id/btnBitmap"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="TestBitmap"
  13. />
  14. <ImageView
  15. android:id="@+id/imageBitmap"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. />
  19. </LinearLayout>

java源代碼:

  1. package com.infy.configuration;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import android.app.Activity;
  5. import android.content.res.AssetManager;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.drawable.BitmapDrawable;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. public class BitmapTest extends Activity{
  14. String[] images = null;
  15. AssetManager assets = null;
  16. int currentImge = 0;
  17. ImageView image;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. // TODO Auto-generated method stub
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.bitmaptest);
  23. image = (ImageView)findViewById(R.id.imageBitmap);
  24. try{
  25. assets = getAssets();
  26. //獲取/assests/目錄下的所有的文件
  27. images = assets.list("");
  28. }catch(IOException e){
  29. e.printStackTrace();
  30. }
  31. final Button next = (Button)findViewById(R.id.btnBitmap);
  32. next.setOnClickListener(new OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. // TODO Auto-generated method stub
  36. if(currentImge >= images.length){
  37. currentImge = 0;
  38. }
  39. //找到下一個圖片文件
  40. while(!images[currentImge].endsWith(".png")){
  41. currentImge++;
  42. //如果發生數組越界
  43. if(currentImge >= images.length){
  44. currentImge = 0;
  45. }
  46. }
  47. InputStream assetFile = null;
  48. try{
  49. //打開指定資源對應的輸入流
  50. assetFile = assets.open(images[currentImge++]);
  51. }catch(IOException e){
  52. e.printStackTrace();
  53. }
  54. BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();
  55. //如果圖片還未回收,先強制回收該圖片
  56. if(bitmapDrawable !=null && !bitmapDrawable.getBitmap().isRecycled()){
  57. bitmapDrawable.getBitmap().recycle();
  58. }
  59. //該變現實的圖片
  60. image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
  61. }
  62. });
  63. }
  64. }

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved