Android图片查看器-从SD卡读取图片
jopen
11年前
本例是先从SD卡读取图片信息,显示在列表视图中;点击图片即可跳转另一个activity查看图片,并且可以浏览图片。
读取图片代码:
import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.provider.MediaStore.Images.Media; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; /** * 图片展示类(列表视图) * @author Administrator * */ public class LookImageActivity extends Activity{ /**列表视图*/ private ListView listView; /**图片对象*/ private ImageView image; /**图片名字*/ private String name; /**图片名字集合*/ private ArrayList<String> list = new ArrayList(); /**图片对象集合*/ private ArrayList<HashMap<String,Object>> picList = new ArrayList(); /** * 初始化 */ public void initData(){ //查询SD卡所有图片,得到游标 Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null); while(cursor.moveToNext()){ //得到图片的名字 String name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME)); //得到图片的描述信息 String info = cursor.getString(cursor.getColumnIndex(Media.DESCRIPTION)); //图片数据 byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); HashMap map = new HashMap(); map.put("name", name==null ? "":name); map.put("info", info==null ? "":info); map.put("data", data); picList.add(map); } cursor.close(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.look_iamge_layout); //调用初始化方法 initData(); listView = (ListView) findViewById(R.id.image_listView); //创建适配器 BaseAdapter adapter = new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView = getLayoutInflater().inflate(R.layout.look_image_listview_content, null); } image = (ImageView) convertView.findViewById(R.id.picture_list_content_iv_pic); TextView nameText = (TextView) convertView.findViewById(R.id.picture_list_content_tv_name); TextView infoText = (TextView) convertView.findViewById(R.id.picture_list_content_tv_info); name = picList.get(position).get("name").toString(); String info = picList.get(position).get("info").toString(); byte[] bs = (byte[]) picList.get(position).get("data"); //设置图片信息 nameText.setText(name); infoText.setText(info); //设置图片 Bitmap bitmap = BitmapFactory.decodeFile(new String(bs,0,bs.length-1)); image.setImageBitmap(bitmap); //设置标识符 image.setTag(position); list.add(name); //为图片添加监听 image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(LookImageActivity.this,LargeImageActivity.class); int id =Integer.parseInt(v.getTag().toString()); intent.putExtra("id", id); intent.putExtra("list", list); startActivity(intent); } }); return convertView; } @Override public long getItemId(int position) { return position; } @Override public Object getItem(int position) { return position; } @Override public int getCount() { return picList.size(); } }; listView.setAdapter(adapter); } }
浏览图片,手势滑动屏幕即可浏览图片;这里用到手势管理器和ontouchEvent()方法。
查看图片代码:
import java.util.ArrayList; import android.app.Activity; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.provider.MediaStore.Images.Media; import android.util.Log; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; /** * 图片查看类(手势管理) * @author Administrator * */ public class LargeImageActivity extends Activity implements OnGestureListener{ /**图片视图*/ private ImageView image; /**图片所在的行号*/ private int id; /**手势管理器*/ private GestureDetector gd; private Cursor cursor; /**图片的字节数组*/ private byte[] data; /**图片名字集合*/ private ArrayList<String> list = new ArrayList(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.llook_image_browse_layout); image = (ImageView) findViewById(R.id.look_imageView_large); //注册手势管理 gd = new GestureDetector(this); //得到行号 id = getIntent().getIntExtra("id", id); //得到传递过来的图片名字集合 list = getIntent().getStringArrayListExtra("list"); //调用设置图片方法 setImage(); } /** * 设置图片 */ public void setImage(){ //到SD卡查询图片信息 cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null,null,null,null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME)); byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); //查询到的图片名字和点击传递过来的图片名字相同时,设置图片 if(name.equals(list.get(id))){ Bitmap bitmap = BitmapFactory.decodeFile(new String(data,0,data.length-1)); image.setImageBitmap(bitmap); } } } @Override public boolean onTouchEvent(MotionEvent event) { //把该事件交给手势管理器来处理 return gd.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { return false; } /** * 对手势操作后组件的处理 */ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //从右往左滑动 if(e1.getX()-e2.getX()>50){ id++; if(id<=cursor.getCount()){ setImage(); }else{ id=0; setImage(); } } //从左往右滑动 if(e1.getX()-e2.getX()<50){ id--; if(id>=0){ setImage(); }else{ id=cursor.getCount(); setImage(); } } return false; } @Override public void onLongPress(MotionEvent e) {} @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) {} @Override public boolean onSingleTapUp(MotionEvent e) { return false; } }