Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用glide平滑地从外部存储器加载图像?_Android_Image_Android Recyclerview_Gallery_Gridlayoutmanager - Fatal编程技术网

Android 如何使用glide平滑地从外部存储器加载图像?

Android 如何使用glide平滑地从外部存储器加载图像?,android,image,android-recyclerview,gallery,gridlayoutmanager,Android,Image,Android Recyclerview,Gallery,Gridlayoutmanager,我想要什么 我想为自己申请一个画廊。 为此,我想通过GridLayoutManager将手机上的每个图像加载到RecyclerView中 什么有效 我用path从外部存储器中获取了所有图像。 第一次启动应用程序时会显示图像 什么不起作用 当我在我的RecyclerView中向下滚动时,Images将被扭曲,如下面的屏幕截图所示 我还通过logcat得到了这个错误: Process: com.spicysoftware.gallery, PID: 12209

我想要什么

我想为自己申请一个画廊。 为此,我想通过GridLayoutManager将手机上的每个图像加载到RecyclerView中

什么有效

我用path从外部存储器中获取了所有图像。 第一次启动应用程序时会显示图像

什么不起作用

当我在我的RecyclerView中向下滚动时,Images将被扭曲,如下面的屏幕截图所示

我还通过logcat得到了这个错误:

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)
这是我加载图像的方式

公共void loadAllImages(){

String[]mProjection={MediaStore.Images.Media.DATE_take,MediaStore.Images.Media.DATA};
Cursor cursorimagesternal=getContentResolver().query(MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI,mProjection,null,null,MediaStore.Images.Media.DATE\u已添加);
List allItems=new ArrayList();
int countImages=0;
while(cursorimagesternal.moveToNext()){
long date=cursorimagesternal.getLong(0);
String fileLoc=cursorimagesternal.getString(1);
添加(新项目对象(fileLoc));
countImages++;
}
Log.v(“图像计数:”,“+countImages”);
列表行列表项=所有项;
lLayout=新的GridLayoutManager(MainActivity.this,4);
RecyclerView rView=(RecyclerView)findViewById(R.id.recycler\u视图);
rView.setHasFixedSize(真);
rView.setLayoutManager(lLayout);
RecycleServiceAdapter rAdapter=新的RecycleServiceAdapter(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();
    }
}
公共类RecycleServiceAdapter扩展了RecyclerView.Adapter{
私人清单项目清单;
私人语境;
公共RecycleServiceAdapter(上下文上下文,列表项列表){
this.itemList=itemList;
this.context=上下文;
}
@凌驾
public RecycleServiceWholders onCreateViewHolder(视图组父级,int-viewType){
View layoutView=LayoutFlater.from(parent.getContext()).flate(R.layout.card\u View\u list,null);
RecyclerViewWholders rcv=新的RecyclerViewWholders(布局视图);
返回rcv;
}
@凌驾
BindViewHolder上的公共无效(最终RecycleriveWholders holder,内部位置){
//holder.countryName.setText(itemList.get(position.getName());
Glide.with(context.asBitmap())
.load(itemList.get(position.getName())
.缩略图(0.5f)
.into(holder.countryPhoto);
}
@凌驾
public int getItemCount(){
返回此.itemList.size();
}
}
卡片视图列表.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.2f或0.1f

你也可以尝试一下,或者用其他的方法用你的投影得到缩略图。并不能保证每个图像都有缩略图。你也需要控制它

在滑动请求中,如果不介意使用回调挂起位图,可以删除
.asBitmap()

你可以像这样膨胀

 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);
获取布局参数

您可以尝试以异步方式加载图像。因此,用户界面将不会挣扎

祝你好运


Emre

您也可以发布
卡片查看列表.xml
吗?当然可以。已经做了!我固定了从ImageView country_photo到92dp的布局宽度和布局高度,以进行测试。现在它可以正常工作了,没有滞后或其他任何问题。R.id.recycler\u view在哪里?如果不为单元格指定固定的大小,glide可能无法理解尺寸以调整大位图的大小并将其保留为原始位图。这也可能是问题所在。我将你的答案标记为解决方案。诀窍是给imageView一个固定的大小!很高兴听到这个消息谢谢
 View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
 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);*/
    }