2016-06-10 2 views
0

Ich verwende eine Bibliothek von Github https://github.com/jaydeepw/poly-picker. Diese Bibliothek zeigt Bilder in LinearLayout dynamisch an. Ich wollte die Bilder in gridView anzeigen. Beim Ausführen der App zeigt die GridView nichts an.Android GridView zeigt alle Bilder, die in einem bestimmten Ordner vorhanden sind

Der Code, den ich verwendete, ist wie folgt.

Alle Erklärungen

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; 
} 

Adapter Code

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; 
} 

Die RetriveCapturedImagePath liest alle Bilder, die in einem bestimmten Ordner befinden. Was ich erreichen möchte ist, wenn ich ein Bild aufnehmen oder ein Bild aus der Galerie auswählen. Nur diese Bilder müssen im Raster angezeigt werden.

Antwort

0

Vielleicht haben Sie falsch auf Ihrem ImageListAdapter Code. Bitte überprüfen Sie Ihren Adapter erneut

+0

Ich habe meinen Adapter-Code hinzugefügt. Ich kann alle Bilder in diesem bestimmten Ordner anzeigen. Aber ich möchte nur die ausgewählten Bilder aus diesem Ordner anzeigen –

Verwandte Themen