照片不会出现在GridView中

时间:2017-02-22 08:10:19

标签: android gridview type-conversion

我从图库中选择了多张照片并在GridView中显示。一切正常,当我选择3张照片时需要3个项目区域,但是在GridView中没有显示照片。我认为数据类型转换问题。

这是图库活动中的选择照片

if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
    List<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
   // StringBuffer stringBuffer = new StringBuffer();
    /*for (Photo photo: mPhotos) {
        System.out.println(photo.getPhoto());
    }*/
    for (int i = 0, l = images.size(); i < l; i++) {
        Photo photos = new Photo();
        photos.setPhoto(String.valueOf(i));
        mPhotos.add(photos);
        stringBuffer.append(images.get(i).path + "\n");
    }
    Log.i("SS", stringBuffer.toString());
    //  textView.setText(stringBuffer.toString());
}

这是适配器

public class SelectPhotoAdapter extends BaseAdapter {
    List<Photo> mPhotos = new ArrayList<Photo>();
    LayoutInflater mInflater;

    public SelectPhotoAdapter(Context context, List<Photo> mPhotos) {
        mInflater = LayoutInflater.from(context);
        this.mPhotos = mPhotos;
    }

    @Override
    public int getCount() {
        return mPhotos.size();
    }

    @Override
    public Photo getItem(int position) {
        return mPhotos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return Integer.valueOf(mPhotos.get(position).photo);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ImageView picture;

        if (v == null) {
            v = mInflater.inflate(R.layout.gridview_item, parent, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
        }

        picture = (ImageView) v.getTag(R.id.picture);
        Photo photo = getItem(position);
        picture.setImageResource(Integer.parseInt(photo.getPhoto()));
        return v;
    }
}

这是xml

<com.example.android.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>

2 个答案:

答案 0 :(得分:0)

试试这个

Bitmap bmp = BitmapFactory.decodeFile(photo.getPhoto());

    // Set the decoded bitmap into ImageView
    picture.setImageBitmap(bmp);

答案 1 :(得分:0)

您需要在清单中添加permisssion:使用PATH读取图像

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

对于Api&gt; = 23,您需要运行时权限:

在活动的onCreate()中写下这个:

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

抓住许可结果:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                finish();
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}

现在你可以使用以下方式设置图像:

Bitmap bmp= BitmapFactory.decodeFile(photo.getPhoto());
ImageView picture = (ImageView)v.findViewById(R.id.picture);
picture.setImageBitmap(bmp);