如何在gridview中显示损坏的图像

时间:2014-04-02 06:43:44

标签: android image gridview

我使用网格视图来显示我的图库图像,但很少有人不显示任何内容。当我打开Facebook应用程序并进行图像发布时,它也会显示这些空白图像,但是当我试图选择它显示消息并且#34;无法读取文件时,它可能会被损坏。&# 34;

我该怎么做?

            final String[] columns = { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        Cursor imagecursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                null, null, orderBy);
        int image_column_index = imagecursor
                .getColumnIndex(MediaStore.Images.Media._ID);
        count = imagecursor.getCount();
        thumbnails = new Bitmap[count];
        arrPath = new String[count];
        thumbnailsselection = new boolean[count];
        for (int i = 0; i < count; i++) {
            try {
                imagecursor.moveToPosition(i);
                int id = imagecursor.getInt(image_column_index);
                int dataColumnIndex = imagecursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                        getApplicationContext().getContentResolver(), id,
                        MediaStore.Images.Thumbnails.MICRO_KIND, null);

                arrPath[i] = imagecursor.getString(dataColumnIndex);

                onProgressUpdate(String.valueOf(id));

            } catch (Exception e) {

            }
        }

这里是我在doInBackground()中使用的代码;

谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

此行用于实际刷新图库项目

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));

它会发生,而一些文件你只是从sdcard删除和android的图库没有更新,即时间设备无法获取该图像,所以你可以试试这个  对于图像

private void getallimages(File dir)
    {

    String[] STAR = { "*" };
    controller.images.clear();

    final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
    Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
    int count = imagecursor.getCount();
    for (int i = 0; i < count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        ImageItem imageItem = new ImageItem();//this is my wrapper class 
        if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760)
        {
            imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));

        imageItem.id = id;
        imageItem.selection = false; //newly added item will be selected by default this it do for check box unselect u dont need to fill this 
        controller.images.add(imageItem);//this i just add all info in wrapper class    

        }
} }

如果它也不会来自这个,那么只需为用户打印一些东西:)

when u try to fetch or open that image then just give one condition like i try to open that image and if it is not present then toast for it that this image might be corrupt or not present in sdcard

答案 1 :(得分:1)

如果您的图库项目显示不正确,那么它可能不会出现在SDCard中,因此您必须刷新图库项目。要刷新,请按照link

进行操作