Android GridView显示特定文件夹中存在的所有图像

时间:2016-06-10 11:44:50

标签: android gridview

我正在使用github https://github.com/jaydeepw/poly-picker中的库。此库动态显示LinearLayout中的图像。我想在gridView中显示图像。在运行应用程序时,gridView不显示任何内容。

我使用的代码如下。

所有声明

private static final String TAG = MainActivity.class.getSimpleName();

private static final int INTENT_REQUEST_GET_IMAGES = 13;
private static final int INTENT_REQUEST_GET_N_IMAGES = 14;

private Context mContext;
View getImages, getNImages;
private GridView mSelectedImagesContainer;
HashSet<Uri> mMedia = new HashSet<Uri>();
private List<String> listOfImagesPath;

public static final String GridView_ImagePath =
       Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/";

 mSelectedImagesContainer = (GridView) findViewById(R.id.selected_photos_container);
    getImages = findViewById(R.id.get_images);

    getImages.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            getImages();
        }
    });

    getNImages = findViewById(R.id.get_n_images);

    getNImages.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            getNImages();
        }
    });
}

private void getImages() {

    Intent intent = new Intent(mContext, ImagePickerActivity.class);
    startActivityForResult(intent, INTENT_REQUEST_GET_IMAGES);
}

private void getNImages() {
    Intent intent = new Intent(mContext, ImagePickerActivity.class);
    Config config = new Config.Builder()
            .setTabBackgroundColor(R.color.white)    // set tab background color. Default white.
            .setTabSelectionIndicatorColor(R.color.blue)
            .setCameraButtonColor(R.color.orange)
            .setSelectionLimit(Integer.MAX_VALUE)// set photo selection limit. Default unlimited selection.
            .build();

    ImagePickerActivity.setConfig(config);
    startActivityForResult(intent, INTENT_REQUEST_GET_N_IMAGES);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == INTENT_REQUEST_GET_IMAGES || requestCode == INTENT_REQUEST_GET_N_IMAGES) {
            Parcelable[] parcelableUris = intent.getParcelableArrayExtra
                    (ImagePickerActivity.EXTRA_IMAGE_URIS);

            if (parcelableUris == null) {
                return;
            }

            // Java doesn't allow array casting, this is a little hack
            Uri[] uris = new Uri[parcelableUris.length];
            System.arraycopy(parcelableUris, 0, uris, 0, parcelableUris.length);

            if (uris != null) {
                for (Uri uri : uris) {
                    Log.i(TAG, " uri: " + uri);
                    mMedia.add(uri);
                }

                showMedia();
            }
        }
    };



private void showMedia() {
    Iterator<Uri> iterator = mMedia.iterator();
    ImageInternalFetcher imageFetcher = new ImageInternalFetcher(this, 500);
    while (iterator.hasNext()) {
        Uri uri = iterator.next();

        // showImage(uri);
        Log.i(TAG, " uri: " + uri);
        if (mMedia.size() >= 1) {
            mSelectedImagesContainer.setVisibility(View.VISIBLE);
        }

        View imageHolder = LayoutInflater.from(this).inflate(R.layout.media_layout, null);

        // View removeBtn = imageHolder.findViewById(R.id.remove_media);
        // initRemoveBtn(removeBtn, imageHolder, uri);
        ImageView thumbnail = (ImageView) imageHolder.findViewById(R.id.media_image);

        if (!uri.toString().contains("content://")) {
            // probably a relative uri
            uri = Uri.fromFile(new File(uri.toString()));
        }

        imageFetcher.loadImage(uri, thumbnail);

        // mSelectedImagesContainer.addView(imageHolder);
        listOfImagesPath = null;
        listOfImagesPath = RetriveCapturedImagePath();

        if (listOfImagesPath != null) {

            mSelectedImagesContainer.setAdapter(new ImageListAdapter(this, listOfImagesPath));

        } else {
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
        }

        // set the dimension to correctly
        // show the image thumbnail.
        int wdpx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
                getResources().getDisplayMetrics());
        int htpx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
                getResources().getDisplayMetrics());
        thumbnail.setLayoutParams(new FrameLayout.LayoutParams(wdpx, htpx));
    }
}

private List<String> RetriveCapturedImagePath() {
    List<String> tFileList = new ArrayList<String>();
    File f = new File(GridViewDemo_ImagePath);
    if (f.exists()) {
        File[] files=f.listFiles();
        Arrays.sort(files);

        for(int i=0; i<files.length; i++){
            File file = files[i];
            if(file.isDirectory())
                continue;
            tFileList.add(file.getPath());
        }
    }
    return tFileList;
}

适配器代码

private Context context;
private List<String> imgPic;
ByteArrayOutputStream bytearrayoutputstream;
public ImageListAdapter(Context c, List<String> thePic)
{
    context = c;
    imgPic = thePic;
}
public int getCount() {
    if(imgPic != null)
        return imgPic.size();
    else
        return 0;
}

//---returns the ID of an item---
public Object getItem(int position) {
    return position;
}

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

//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
    bytearrayoutputstream = new ByteArrayOutputStream();
    ImageView imageView;
    BitmapFactory.Options bfOptions=new BitmapFactory.Options();
    bfOptions.inDither=false;                     //Disable Dithering mode
    bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    bfOptions.inTempStorage=new byte[32 * 800];
    if (convertView == null) {
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setPadding(5, 5, 5, 5);
    } else {
        imageView = (ImageView) convertView;
    }
    FileInputStream fs = null;
    Bitmap bm;
    try {

        fs = new FileInputStream(new File(imgPic.get(position)));

        if(fs!=null) {
            //bm = ((BitmapDrawable)drawable).getBitmap();

            bm=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
            bm.compress(Bitmap.CompressFormat.JPEG,40,bytearrayoutputstream );

            imageView.setImageBitmap(bm);
            imageView.setId(position);
            imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        if(fs!=null) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return imageView;
}

RetriveCapturedImagePath读取特定文件夹中的所有图像。 我想要实现的是当我捕捉图像或从图库中选择图像时。只有图像必须显示在网格中。

1 个答案:

答案 0 :(得分:0)

您的 ImageListAdapter 代码可能存在错误。请再次检查您的适配器