android 根據SD卡中圖片路徑讀取並顯示SD中的圖片——源代碼

package com.guo; 
 
import java.io.File; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 
 
public class MainAct extends Activity { 
        /** Called when the activity is first created. */ 
        private ImageView img; 
        //SD圖片路徑  
        private String filepath = "/sdcard/hot.png"; 
 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.main); 
                img = (ImageView) findViewById(R.id.img); 
                File file = new File(filepath); 
                if (file.exists()) { 
                        Bitmap bm = BitmapFactory.decodeFile(filepath); 
                        //將圖片顯示到ImageView中  
                        img.setImageBitmap(bm); 
                } 
        } 

package com.guo;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainAct extends Activity {
        /** Called when the activity is first created. */
        private ImageView img;
        //SD圖片路徑
        private String filepath = "/sdcard/hot.png";

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                img = (ImageView) findViewById(R.id.img);
                File file = new File(filepath);
                if (file.exists()) {
                        Bitmap bm = BitmapFactory.decodeFile(filepath);
                        //將圖片顯示到ImageView中
                        img.setImageBitmap(bm);
                }
        }
}

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *