例子1:android加載自己文件夾的圖片,使用R類

           

          main.xml文件

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              >
          <ImageView
               android:id="@+id/iv1"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:src="@drawable/longshuai"      <!-- longshuai.png為圖片的名稱,記在資源文件里頭,不用文件名后綴-->
             />
          </LinearLayout>

          注意:強(qiáng)調(diào)一下,資源文件的圖片命名規(guī)則比較嚴(yán)格,由[a-z]和數(shù)字和“_”組成,而且不能數(shù)字開頭,我就常犯傻,命名老是數(shù)字或者大寫字母開頭,這種錯(cuò)誤——囧。。

          我們要把longshuai.png導(dǎo)入到res中,最簡單的方式就是直接找到這個(gè)文件夾,復(fù)制進(jìn)去

          之后右鍵更新,我們就可以在res中看到自己的圖片了

          android使用ImageView加載本地SdCard圖片和加載網(wǎng)絡(luò)圖片 - 夏天的風(fēng) - FreeSimpleHappy

           

          不用寫代碼。。直接用自動(dòng)生成的代碼。。

               public void onCreate(Bundle savedInstanceState)

              {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);

                }

          效果如下:

          android使用ImageView加載本地SdCard圖片和加載網(wǎng)絡(luò)圖片 - 夏天的風(fēng) - FreeSimpleHappy

           

           

          2、加載本地圖片(其實(shí)主要是SdCard中圖片)

          關(guān)于SdCard的使用,可以參見 http://longshuai2007.blog.163.com/blog/static/1420944142011611103950500/

           

          xml文件同上面的是一樣的,并不需要修改

              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
                 
                  ImageView image1 = (ImageView) findViewById(R.id.iv1);  //獲得ImageView對(duì)象
                   /*為什么圖片一定要轉(zhuǎn)化為 Bitmap格式的!! */
                  Bitmap bitmap = getLoacalBitmap("/sdcard/tubiao.jpg"); //從本地取圖片(在cdcard中獲取)  //
                  image1 .setImageBitmap(bitmap); //設(shè)置Bitmap
                }

           

              /**
              * 加載本地圖片
              * @param url
              * @return
              */
              public static Bitmap getLoacalBitmap(String url) {
                   try {
                        FileInputStream fis = new FileInputStream(url);
                        return BitmapFactory.decodeStream(fis);  ///把流轉(zhuǎn)化為Bitmap圖片        

                     } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        return null;
                   }
              }

           

          顯示效果如下:

          android使用ImageView加載本地SdCard圖片和加載網(wǎng)絡(luò)圖片 - 夏天的風(fēng) - FreeSimpleHappy
           
           
          3、獲取網(wǎng)絡(luò)上的圖片
          mian.xml文件和1中一樣,并不需要修改!
           
          代碼:
              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
                 
                  ImageView image1 = (ImageView) findViewById(R.id.iv1);
                  Bitmap bitmap =getHttpBitmap("
                                 //從網(wǎng)上取圖片
                  image1 .setImageBitmap(bitmap); //設(shè)置Bitmap
          }
           
              /**
              * 從服務(wù)器取圖片
              * @param url
              * @return
              */
              public static Bitmap getHttpBitmap(String url) {
                   URL myFileUrl = null;
                   Bitmap bitmap = null;
                   try {
                        myFileUrl = new URL(url);
                   } catch (MalformedURLException e) {
                        e.printStackTrace();
                   }
                   try {
                        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
                        conn.setConnectTimeout(0);
                        conn.setDoInput(true);
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        bitmap = BitmapFactory.decodeStream(is);
                        is.close();
                   } catch (IOException e) {
                        e.printStackTrace();
                   }
                   return bitmap;
              }
           
           
          注意:由于需要使用網(wǎng)絡(luò),所以 AndroidManfest.xml 文件需要添加使用網(wǎng)絡(luò)權(quán)限!!!
              <uses-permission android:name="android.permission.INTERNET" />
           
          這也是我為什么一直錯(cuò),而且一直找不到原因,它就是不顯示圖片,什么錯(cuò)誤都沒有,坑爹啊!!
           
          android使用ImageView加載本地SdCard圖片和加載網(wǎng)絡(luò)圖片 - 夏天的風(fēng) - FreeSimpleHappy
           
          Comments
          • # re: android使用ImageView加載本地SdCard圖片和加載網(wǎng)絡(luò)圖片
            luoshuisanqian
            Posted @ 2016-04-11 15:59
            你導(dǎo)包正確嗎,我試過那段代碼了沒問題啊,可以加載網(wǎng)絡(luò)圖片  回復(fù)  更多評(píng)論   
          • # re: android使用ImageView加載本地SdCard圖片和加載網(wǎng)絡(luò)圖片
            luoshuisanqian
            Posted @ 2016-04-11 16:02
            HttpURLConnection這個(gè)類你是導(dǎo)這個(gè)包嗎java.net.HttpURLConnection
              回復(fù)  更多評(píng)論   

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 柳州市| 邢台县| 商南县| 米易县| 冕宁县| 冷水江市| 上思县| 永州市| 洞头县| 渝中区| 雅安市| 衢州市| 夏津县| 三台县| 左权县| 石台县| 仁化县| 和硕县| 平泉县| 邹平县| 义乌市| 茌平县| 东至县| 河北省| 新和县| 西乌| 靖远县| 施秉县| 诏安县| 庆城县| 嘉黎县| 化州市| 五大连池市| 溧水县| 江达县| 平湖市| 南川市| 库尔勒市| 望城县| 庄浪县| 介休市|