亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我自己寫了一個用listview加載本地內存卡的圖片,預期效果是顯示所有的本地內存卡的圖片,而結果卻是在listview中顯示幾張圖片,下面是我寫的代碼,求指教

我自己寫了一個用listview加載本地內存卡的圖片,預期效果是顯示所有的本地內存卡的圖片,而結果卻是在listview中顯示幾張圖片,下面是我寫的代碼,求指教

ArryLuo 2015-12-19 10:19:29
public?class?MainActivity?extends?Activity?{ private?ImageView?imageView; private?ListView?listView; List<Bitmap>?list?=?GetBitmap.getbitmap(); @Override protected?void?onCreate(Bundle?savedInstanceState)?{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView?=?(ListView)?findViewById(R.id.listview); new?Myasynctak().execute("mnt/sdcard"); } class?Myasynctak?extends?AsyncTask<String,?Void,?List<ImgBen>>?{ @Override protected?List<ImgBen>?doInBackground(String...?params)?{ //?TODO?Auto-generated?method?stub return?getUrl(params[0]); } @Override protected?void?onPostExecute(List<ImgBen>?result)?{ //?TODO?Auto-generated?method?stub super.onPostExecute(result); MyAdpter?adpter=new?MyAdpter(MainActivity.this,?result); listView.setAdapter(adpter); } } private?List<ImgBen>?getUrl(String?url)?{ List<ImgBen>?list?=?new?ArrayList<ImgBen>(); File?file?=?new?File(url); //?判斷是否是文件 if?(file.exists())?{ File[]?arry?=?file.listFiles(); for?(int?i?=?0;?i?<?arry.length;?i++)?{ String?name?=?arry[i].getName();//?獲取這個文件的名稱 if?(name.endsWith(".jpg")?||?name.endsWith(".png"))?{ //?獲取他的絕對路徑 String?filepath?=?arry[i].getAbsolutePath(); ImgBen?ben?=?new?ImgBen(filepath); list.add(ben); } } } return?list; } }//這個Javabeanpublic class ImgBen {private String url;public ImgBen() { super(); // TODO Auto-generated constructor stub}public ImgBen(String url) { super(); this.url = url;}public String getUrl() { return url;}public void setUrl(String url) { this.url = url;}}//這個是加載圖片的異步操作public class LoginThread { ImageView mimageView; Handler mhandler=new Handler(){ public void handleMessage(android.os.Message msg) { mimageView.setImageBitmap((Bitmap) msg.obj); }; }; public void showThread(ImageView imageView, final String url) { mimageView=imageView; new Thread(){ @Override public void run() { // TODO Auto-generated method stub super.run(); Bitmap bitmap=getbitmapImage(url); Message message=new Message(); message.obj=bitmap; mhandler.sendMessage(message); } }.start(); } public Bitmap getbitmapImage(String url){ Bitmap bitmap=null; FileInputStream fis=null; BufferedInputStream bis=null; try { fis=new FileInputStream(url); bis=new BufferedInputStream(fis); bitmap=BitmapFactory.decodeStream(bis); return bitmap; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }}//這個是我適配器public class MyAdpter extends BaseAdapter { private LayoutInflater inflater; private List<ImgBen>list; public MyAdpter(Context context,List<ImgBen>list) { inflater = LayoutInflater.from(context); this.list=list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.item, null); ImgBen bitmap=list.get(position); ImageView imageView= (ImageView) view.findViewById(R.id.image); new LoginThread().showThread(imageView, bitmap.getUrl()); return view; }}
查看完整描述

2 回答

已采納
?
Hello橙子

TA貢獻13條經驗 獲得超0個贊

你看看你在獲取所有圖片路徑的時候是不是遍歷了根目錄所有的文件夾和子文件夾。getUrl(String?url)就是這個方法。

查看完整回答
反對 回復 2015-12-20
?
ArryLuo

TA貢獻3條經驗 獲得超2個贊

在MainActivity中的List<Bitmap>?list?=?GetBitmap.getbitmap();是沒用的,剛剛提交的時候忘了把他注釋掉

查看完整回答
反對 回復 2015-12-19
  • 2 回答
  • 0 關注
  • 1699 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號