检索Android(安卓)SDCard的图片,并显示到imagebutton
16lz
2021-01-24
注意:如果你在模拟器已经启动的情况下,push了几张图片到SDCard中[试过:mnt/sdcard/Picture路径下],建议将模拟器关了,再重新启动一下,否则,刚刚添加的图片,是没有办法获取到的。这是因为Android是在系统启动的时候来扫描模拟器上SDCard中多媒体文件的。
代码:
[java] view plain copy print ?
- importandroid.app.Activity;
- importandroid.database.Cursor;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.os.Bundle;
- importandroid.provider.MediaStore.Images.Media;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.widget.ImageButton;
- importandroid.widget.TextView;
- publicclassPhotoManageextendsActivity{
- privatestaticfinalfloatDISPLAY_WIDTH=200;
- privatestaticfinalfloatDISPLAY_HEIGHT=200;
- privateImageButtonphotoView;
- privateTextViewnameView;
- privateCursorcursor;
- privateStringphotoPath;
- privateBitmapcurrPhoto;
- //三个变量用来保存Media.DATA,Media.TITLE,Media.DISPLAY_NAME的索引号,来获取每列的数据
- privateintphotoIndex;
- privateintnameIndex;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- photoView=(ImageButton)this.findViewById(R.id.image_view);
- photoView.setOnClickListener(clickListener);
- nameView=(TextView)this.findViewById(R.id.view_name);
- //指定获取的列
- Stringcolumns[]=newString[]{
- Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME
- };
- cursor=this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI,columns,null,null,null);
- photoIndex=cursor.getColumnIndexOrThrow(Media.DATA);
- nameIndex=cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
- Log.v("HEREFirst:","FirstDebug");
- //显示第一张图片,但是首先要判断一下,Cursor是否有值
- if(cursor.moveToFirst()){
- showImage();
- }
- }
- privateView.OnClickListenerclickListener=newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- if(cursor.moveToNext()){
- showImage();
- }
- }
- };
- /**
- *显示图像
- */
- privatevoidshowImage(){
- photoPath=cursor.getString(photoIndex);//获取图片存储位置
- Log.v("PhotoPath:",photoPath);
- currPhoto=decodeBitmap(photoPath);
- photoView.setImageBitmap(currPhoto);
- nameView.setText(cursor.getString(nameIndex));
- }
- /**
- *从path中获取图片信息
- *@parampath
- *@return
- */
- privateBitmapdecodeBitmap(Stringpath){
- BitmapFactory.Optionsop=newBitmapFactory.Options();
- op.inJustDecodeBounds=true;
- Bitmapbmp=BitmapFactory.decodeFile(path,op);//获取尺寸信息
- //获取比例大小
- intwRatio=(int)Math.ceil(op.outWidth/DISPLAY_WIDTH);
- inthRatio=(int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);
- //如果超出指定大小,则缩小相应的比例
- if(wRatio>1&&hRatio>1){
- if(wRatio>hRatio){
- op.inSampleSize=wRatio;
- }else{
- op.inSampleSize=hRatio;
- }
- }
- op.inJustDecodeBounds=false;
- bmp=BitmapFactory.decodeFile(path,op);
- returnbmp;
- }
- }
[html] view plain copy print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <ImageButton
- android:id="@+id/image_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/view_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
更多相关文章
- Pyqt adb 获取Android手机屏幕
- Android的ImageButton问题
- Android(安卓)scaleType属性与ImagView中图片的显示的关系
- Android开发需要掌握的10个工具
- Android(安卓)拍照:如何使用已有相机应用捕获图片(Taking Photos S
- android和OPhone模拟器界面对比
- Android中 在显示ImageView时图片上面和下面都出现一段空白区间
- Android自动化工具Monkeyrunner使用(一)
- Android画图并保存图片到本地