2016-05-10 4 views
0

Ich möchte alle Fotos aus der Galerie holen und in der Rasteransicht anzeigen ..alle Fotos bekomme ich aber das Problem ist, dass es nicht reibungslos funktioniert wie als Galerie zu mach mach mobile hängen Bitte geben Sie mir eine Lösung, wenn jemand meinen Code unten hat.alle Fotos aus der Galerie holen und in der Rasteransicht zeigen

public class getImageFromGallery extends AsyncTask<String,Void,ArrayList<String>> { 

     @Override 
     protected ArrayList<String> doInBackground(String... strings) { 
      Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
      String[] projection = {MediaStore.Images.ImageColumns.DATA}; 
      Cursor c = null; 
      SortedSet<String> dirList = new TreeSet<String>(); 
      final ArrayList<String> resultIAV = new ArrayList<String>(); 

      String[] directories = null; 

      if (u != null) 
      { 
       String sortOrder ="_id asc limit 1"; 
       c = managedQuery(u, projection, null, null, null); 
      } 

      if ((c != null) && (c.moveToFirst())) 
      { 
       do 
       { 
        String tempDir = c.getString(0); 
        tempDir = tempDir.substring(0, tempDir.lastIndexOf("/")); 
        try{ 
         dirList.add(tempDir); 
        } 
        catch(Exception e) 
        { 
        } 
       } 
       while (c.moveToNext()); 
       directories = new String[dirList.size()]; 
       dirList.toArray(directories); 

      } 

      for(int i=0;i<dirList.size();i++) 
      { 
       File imageDir = new File(directories[i]); 
       File[] imageList = imageDir.listFiles(); 
       if(imageList == null) 
        continue; 
       for (File imagePath : imageList) { 
        try { 

         if(imagePath.isDirectory()) 
         { 
          imageList = imagePath.listFiles(); 

         } 
         if (imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG") 
           || imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG") 
           || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG") 
           || imagePath.getName().contains(".gif") || imagePath.getName().contains(".GIF") 
           || imagePath.getName().contains(".bmp") || imagePath.getName().contains(".BMP") 
           ) 
         { 



          String path= imagePath.getAbsolutePath(); 
          resultIAV.add(path); 

         } 
        } 
        // } 
        catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

      ShareMoment.this.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        shareMomentAdapter = new ShareMomentAdapter(ShareMoment.this,resultIAV); 
        sharemomentList.setAdapter(shareMomentAdapter); 
       } 
      }); 

      return resultIAV; 

     } 
    } 

public class ShareMomentAdapter extends RecyclerView.Adapter<ShareMomentAdapter.ShareMomentViewHolder> { 
    private Context context; 
    private ArrayList<String> shareMomentList; 
    ByteArrayOutputStream bytearrayoutputstream; 

    public ShareMomentAdapter(Context context, ArrayList<String> shareMomentList) { 
     this.shareMomentList = shareMomentList; 
//  shareMomentList = new ArrayList<>(); 
//  for (int i = 0; i < 10; i++) { 
//   shareMomentList.add("" + i); 
//  } 
     this.context = context; 
     bytearrayoutputstream = new ByteArrayOutputStream(); 
    } 

    @Override 
    public ShareMomentAdapter.ShareMomentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.item_sharemoments, parent, false); 


     return new ShareMomentViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final ShareMomentAdapter.ShareMomentViewHolder holder, int position) { 
     Bitmap yourSelectedImage = BitmapFactory.decodeFile(shareMomentList.get(position)); 
     final Bitmap resizedImage = Bitmap.createScaledBitmap(yourSelectedImage, 150, 150, true); 

     Glide.with(context).load(shareMomentList.get(position)).diskCacheStrategy(DiskCacheStrategy.RESULT).crossFade().into(holder.shareMomentImage); 
    } 

    @Override 
    public int getItemCount() { 
     return shareMomentList.size(); 
    } 

    public class ShareMomentViewHolder extends RecyclerView.ViewHolder { 
     @Bind(R.id.shareMomentImage) 
     ImageView shareMomentImage; 
     //  @Bind(R.id.nameTextview) 
//  TextView nameTextview; 
//  @Bind(R.id.descriptionTextView) 
//  TextView descriptionTextView; 
     private View view; 

     public ShareMomentViewHolder(View view) { 
      super(view); 
      this.view = view; 
      ButterKnife.bind(this, view); 
     } 
    } 


    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 
     int width = bm.getWidth(); 
     int height = bm.getHeight(); 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 
     Matrix matrix = new Matrix(); 
     matrix.postScale(scaleWidth, scaleHeight); 
     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, 
       matrix, false); 

     return resizedBitmap; 
    } 
} 
+0

Verwenden Sie nicht 'runOnUiThread'. Stattdessen überschreiben Sie 'onPostExecute', das ausgelöst wird, sobald' doInBackground' den Job beendet und das Ergebnis an 'onPostExcecute' zurücksendet. –

+0

Sie sollten nicht alle Fotos gleichzeitig laden. Versuchen Sie, 10 Fotos gleichzeitig zu laden, wenn der Benutzer nach unten scrollt, dann die nächsten 10 Fotos lädt usw. –

+0

Verwenden Sie einen Bildlader wie Picasso oder Glide, der das Caching und das asynchrone Laden für Sie übernimmt. –

Antwort

0

Verwenden Sie LoadMoreView, anstatt alle Bilder gleichzeitig zu laden. Laden Sie jeweils 10-20 Bilder und laden Sie dann mehr, wenn der Benutzer nach unten scrollt. Dies wird viel Speicher sparen.

+0

können Sie mir Code dafür geben? –

+0

https://guides.codepath.com/android/Endless-Scrolling-with-AdapterViews-and-RecyclerView –

Verwandte Themen