无法在GridView正常查看图片 [英] Unable to view images properly in a GridView

查看:107
本文介绍了无法在GridView正常查看图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MainActivity:

 公共类AndroidCustomGalleryActivity延伸活动{
    私人诠释计数;
    私人位图[]的缩略图;
    私人布尔[] thumbnailsselection;
    私有String [] arrPath;
    私人ImageAdapter imageAdapter;
    ArrayList的<串GT; F =新的ArrayList<串GT;(); //文件路径列表
    文件[]的文件列表;
公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_android_custom_gallery);
        getFromSdcard();
        GridView控件imagegrid =(GridView控件)findViewById(R.id.PhoneImageGrid);
        imageAdapter =新ImageAdapter();
        imagegrid.setAdapter(imageAdapter);
    }
    公共无效getFromSdcard()
    {
        档案文件=新的文件(android.os.Environment.getExternalStorageDirectory(),SnapBoard);            如果(file.isDirectory())
            {
                的文件列表= file.listFiles();
                的for(int i = 0; I< listFile.length;我++)
                {                    f.add(listfile中[I] .getAbsolutePath());                }
            }
    }    公共类ImageAdapter延伸BaseAdapter {
        私人LayoutInflater mInflater;        公共ImageAdapter(){
            mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }        公众诠释的getCount(){
            返回f.size();
        }        公共对象的getItem(INT位置){
            返回的位置;
        }        众长getItemId(INT位置){
            返回的位置;
        }        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            ViewHolder持有人;
            如果(convertView == NULL){
                持有人=新ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.galleryitem,NULL);
                holder.imageview =(ImageView的)convertView.findViewById(R.id.thumbImage);                convertView.setTag(保持器);
            }
            其他{
                支架=(ViewHolder)convertView.getTag();
            }
            位图MYBITMAP = BitmapFactory.de codeFILE(f.get(位置));
            holder.imageview.setImageBitmap(MYBITMAP);
            返回convertView;
        }
    }
    类ViewHolder {
        ImageView的ImageView的;
    }
        }

XML:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
< GridView控件
    机器人:ID =@ + ID / PhoneImageGrid
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:columnWidth时=90dp
    机器人:比重=中心
    机器人:horizo​​ntalSpacing =10dp
    机器人:为numColumns =auto_fit
    机器人:stretchMode =columnWidth时
    机器人:verticalSpacing =10dp/>
< / RelativeLayout的>

GalleryItem.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    < ImageView的
        机器人:ID =@ + ID / thumbImage
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerInParent =真/>    <复选框
        机器人:ID =@ + ID / itemCheckBox
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真/>< / RelativeLayout的>

快照:

我能够从我的SD卡检索从单独的文件夹图像,并在网格视图中显示,但该观点似乎相当崩溃。它是否有事情做与屏幕尺寸?我使用的是银河ACE DUOS(3.5英寸)。将尝试发布不久的屏幕截图。帮助将非常AP preciated。先谢谢了。

编辑:

logcat的:

  11月12日至19日:08:25.335:D / dalvikvm(22171):GC_EXTERNAL_ALLOC释放66K,47%免费2957K / 5511K,58486K外部/ 58486K,暂停39MS
十一月12日至19日:08:25.351:E / dalvikvm堆(22171):9830400字节外部分配太大,这一进程。
十一月12日至19日:08:25.390:E / GraphicsJNI(22171):虚拟机不会让我们分配9830400字节
十一月12日至19日:08:25.390:D / dalvikvm(22171):GC_FOR_MALLOC释放< 1K,47%免费2957K / 5511K,58486K外部/ 58486K,暂停30毫秒
十一月12日至19日:08:25.390:D / Skia的(22171):---德codeR->德code返回false


解决方案

我是这么认为的你的问题是,当乌尔越来越网格视图的观点有错误。

错误

 公开对象的getItem(INT位置){
        返回的位置;
}

校正

 公开对象的getItem(INT位置){
        返回f.get(位置);
}

这个错误是因为每当你显示图像,分配虚拟内存中的图像大小。这可以通过调整该位图和减小尺寸,然后显示图像被重写。

 公共位图getResizedBitmap(位图BM,诠释newHeight,诠释newWidth){
INT宽度= bm.getWidth();
INT高度= bm.getHeight();
浮动scaleWidth =((浮点)newWidth)/宽度;
浮动scaleHeight =((浮点)newHeight)/身高;
//创建用于操纵矩阵
字模=新的Matrix();
//调整位图
matrix.postScale(scaleWidth,scaleHeight);//重建新位图
位图resizedBitmap = Bitmap.createBitmap(BM,0,0,宽度,高度,矩阵,假);
返回resizedBitmap;
}

在您的code

 位图myresizedbitmap = getResizedBitmap(MYBITMAP,300,300);
holder.imageview.setImageBitmap(myresizedbitmap);

MainActivity:

public class AndroidCustomGalleryActivity extends Activity {
    private int count;
    private Bitmap[] thumbnails;
    private boolean[] thumbnailsselection;
    private String[] arrPath;
    private ImageAdapter imageAdapter;
    ArrayList<String> f = new ArrayList<String>();// list of file paths
    File[] listFile;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_custom_gallery);
        getFromSdcard();
        GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
        imageAdapter = new ImageAdapter();
        imagegrid.setAdapter(imageAdapter);


    }
    public void getFromSdcard()
    {
        File file= new File(android.os.Environment.getExternalStorageDirectory(),"SnapBoard");

            if (file.isDirectory())
            {
                listFile = file.listFiles();


                for (int i = 0; i < listFile.length; i++)
                {

                    f.add(listFile[i].getAbsolutePath());

                }
            }
    }

    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return f.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }


            Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
            holder.imageview.setImageBitmap(myBitmap);
            return convertView;
        }
    }
    class ViewHolder {
        ImageView imageview;


    }
        }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


<GridView
    android:id="@+id/PhoneImageGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />


</RelativeLayout>

GalleryItem.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <CheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

SnapShot:

I am able to retrieve the images from a seperate folder from my SD card and display it in a grid view but the view seems rather collapsed. Does it has something to do with screen size ? I am using a Galaxy ACE DUOS (3.5 inch). Will try to post the screen shot shortly. Help will be much appreciated. Thanks in advance.

EDIT:

Logcat:

12-19 11:08:25.335: D/dalvikvm(22171): GC_EXTERNAL_ALLOC freed 66K, 47% free 2957K/5511K, external 58486K/58486K, paused 39ms
12-19 11:08:25.351: E/dalvikvm-heap(22171): 9830400-byte external allocation too large for this process.
12-19 11:08:25.390: E/GraphicsJNI(22171): VM won't let us allocate 9830400 bytes
12-19 11:08:25.390: D/dalvikvm(22171): GC_FOR_MALLOC freed <1K, 47% free 2957K/5511K, external 58486K/58486K, paused 30ms
12-19 11:08:25.390: D/skia(22171): --- decoder->decode returned false

解决方案

I think so your problem is when ur are getting the view of grid view there is mistake.

Error

public Object getItem(int position) {
        return position;
}

Correction

public Object getItem(int position) {
        return f.get(position);
}

The error is because whenever you display image, you allocate the image size in Virtual memory. This can be overridden by resizing the bitmap and reducing the size and then display the image.

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);

// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}

In your Code

Bitmap myresizedbitmap= getResizedBitmap(myBitmap,300, 300);
holder.imageview.setImageBitmap(myresizedbitmap);

这篇关于无法在GridView正常查看图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆