0

내가 원하는 내용외부 저장소에서 이미지를 원활하게로드하려면 어떻게해야합니까?

나 자신을 위해 자신의 갤러리 응용 프로그램을 greate하고 싶습니다. 전화의 모든 이미지를 GridLayoutManager에 의해 RecyclerView에로드하려고합니다.

작업

내가 경로와 외부 저장 장치에서 모든 이미지를 가지고 무엇을하는지. 앱이 처음 시작될 때 이미지가 표시됩니다. 내 RecyclerView에서 아래로 스크롤하면

immages 아래 스크린 샷처럼 왜곡됩니다 작동하지 않습니다 무엇

.

enter image description here

또한 로그 캣하여이 오류를 가지고 : 나는 이미지를로드하는 방법

Process: com.spicysoftware.gallery, PID: 12209 
                      java.lang.RuntimeException: Canvas: trying to draw too large(115650308bytes) bitmap. 
                       at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java) 
                       at android.graphics.Canvas.drawBitmap(Canvas.java) 
                       at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java) 
                       at android.widget.ImageView.onDraw(ImageView.java) 
                       at android.view.View.draw(View.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.View.draw(View.java) 
                       at android.view.ViewGroup.drawChild(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.View.draw(View.java) 
                       at android.view.ViewGroup.drawChild(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java) 
                       at android.view.View.draw(View.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.View.draw(View.java) 
                       at android.view.ViewGroup.drawChild(ViewGroup.java) 
                       at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4477) 
                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java) 
                       at android.view.View.draw(View.java) 
                       at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3869) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java) 
                       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java) 
                       at android.view.View.updateDisplayListIfDirty(View.java) 
                       at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java) 
                       at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java) 
                       at android.view.ThreadedRenderer.draw(ThreadedRenderer.java) 
                       at android.view.ViewRootImpl.draw(ViewRootImpl.java) 
                       at android.view.ViewRootImpl.performDraw(ViewRootImpl.java) 
                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java) 
                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java) 
                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java) 
                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java) 
                       at android.view.Choreographer.doCallbacks(Choreographer.java) 
                       at android.view.Choreographer.doFrame(Choreographer.java) 
                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java) 
                       at android.os.Handler.handleCallback(Handler.java) 
                       at android.os.Handler.dispatchMessage(Handler.java) 
                       at android.os.Looper.loop(Looper.java) 
                       at android.app.ActivityThread.main(ActivityThread.java) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 

이이

공공 무효 loadAllImages() {

String[] mProjection = {MediaStore.Images.Media.DATE_TAKEN,MediaStore.Images.Media.DATA}; 
Cursor cursorImagesExternal = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,mProjection,null,null,MediaStore.Images.Media.DATE_ADDED); 
List<ItemObject> allItems = new ArrayList<ItemObject>(); 
int countImages = 0; 

while(cursorImagesExternal.moveToNext()) { 
    long date = cursorImagesExternal.getLong(0); 
    String fileLoc = cursorImagesExternal.getString(1); 
    allItems.add(new ItemObject(fileLoc)); 
    countImages ++; 
} 

Log.v("Image Count: ", ""+countImages); 

List<ItemObject> rowListItem = allItems; 
lLayout = new GridLayoutManager(MainActivity.this, 4); 

RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view); 
rView.setHasFixedSize(true); 
rView.setLayoutManager(lLayout); 

RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem); 
rView.setAdapter(rcAdapter); 

} 

이 어댑터

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> { 

    private List<ItemObject> itemList; 
    private Context context; 

    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) { 
     this.itemList = itemList; 
     this.context = context; 
    } 

    @Override 
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { 

     View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null); 
     RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView); 
     return rcv; 
    } 

    @Override 
    public void onBindViewHolder(final RecyclerViewHolders holder, int position) { 
     //holder.countryName.setText(itemList.get(position).getName()); 

     Glide.with(context).asBitmap() 
       .load(itemList.get(position).getName()) 
       .thumbnail(0.5f) 
       .into(holder.countryPhoto); 


    } 

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

card_view_list.xml

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/card_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 
    card_view:cardUseCompatPadding="true" 
    android:layout_marginBottom="16dp"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/country_photo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/action_settings" 
      android:src="@drawable/ic_remove_red_eye_black_24dp" 
      android:scaleType="centerCrop" /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

내 질문

원활의 GridView에 이미지를로드 할 수있는 더 나은 방법이 있나요입니까?

그렇지 않으면 내 코드에 어떤 문제가 있습니까?

+0

당신도'card_view_list.xml'를 게시 할 수 있습니까? –

+0

물론. 이미 마쳤어! – MSeiz5

+0

테스트를 위해 ImageView country_photo에서 layout_width 및 layout_height를 92dp로 고정했습니다. 이제는 지연이나 다른 작업없이 작동합니다. – MSeiz5

답변

1

썸네일 플로트를 0.2f 또는 0.1f와 같이 낮추어야 할 수도 있습니다.

또한 MediaStore.Images.Thumbnails 또는 다른 방법으로 프로젝션과 함께 축소판 그림을 얻을 수 있습니다. 모든 이미지에 미리보기 이미지가 있다는 보장은 없습니다. 당신도 그것을 제어해야합니다.

비트 맵을 사용하여 콜백을 사용하지 않으려는 경우 글라이드 요청에서 .asBitmap()을 제거 할 수 있습니다.

당신은 레이아웃 PARAMS를 얻을 수

View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, parent, false); 

대신

View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null); 

처럼 팽창 할 수 있습니다.

비동기로 이미지를로드하려면 CursorLoaders 시도해보십시오. 따라서 UI는 어려움을 겪지 않을 것입니다.

행운

엠레

+0

해결책으로 답을 표시했습니다. 트릭은 imageView에 고정 된 크기를주는 것입니다! – MSeiz5

+1

잘 듣고 싶습니다. :) 감사 –

0
int defaultImagePath = R.drawable.default_thumb; 
int errorImagePath = R.drawable.damaged_image; 
holder.countryPhoto.setImageResource(defaultImagePath); 

String uri = itemList.get(position).getName(); 
loadImageWithGlide(mContext, holder.countryPhoto, uri, defaultImagePath, 
         errorImagePath); 



public static void loadImageWithGlide(final Context context, ImageView theImageViewToLoadImage, 
              String theLoadImagePath, int theDefaultImagePath, int tehErrorImagePath) { 
     if (context == null) return; 

     //placeHolderUrl=R.drawable.ic_user; 
     //errorImageUrl=R.drawable.ic_error; 
     Glide.with(context) //passing context 
       .load(theLoadImagePath) //passing your url to load image. 
       .placeholder(theDefaultImagePath) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url. 
       .error(tehErrorImagePath)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is. 
       .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast. 
       //.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image. 
       //.fitCenter()//this method help to fit image into center of your ImageView 
       .into(theImageViewToLoadImage); //pass imageView reference to appear the image. 

     /* Normal way to Load Image with Glide. 
       Glide.with(theContext) 
       .load(theImagePath) 
       .diskCacheStrategy(DiskCacheStrategy.ALL) 
       .into(theImageView);*/ 
    } 
관련 문제