2016-06-10 73 views
0

我正在使用github的庫https://github.com/jaydeepw/poly-picker。該庫動態地在LinearLayout中顯示圖像。我想在gridView中顯示圖像。在運行應用程序時,gridView什麼都不顯示。Android 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讀取所有屬於特定文件夾中的圖像。 我想獲得的是當我捕捉圖像或從圖庫中選擇圖像。只有該圖像必須顯示在網格中。

回答

0

也許你在你的問題上有錯誤ImageListAdapter代碼。請再次檢查您的適配器

+0

我已經添加了我的適配器代碼。我能夠顯示該特定文件夾中的所有圖像。但我想只顯示該文件夾中選定的圖像 –

相關問題