为什么我的图库应用程序(使用Glide设计)占用了近500mb RAM

时间:2019-06-19 17:53:34

标签: android android-layout android-recyclerview android-glide android-memory

我制作了一个图库应用,使用recyclerView中的GLIDE库显示脱机存储的图像,但是当我在android Studio中查看 Profiler时,它显示该应用占用了近500mb的内存。我的应用占用了这么多内存。

但是我的应用程序没有崩溃,但是在显示图像时有时会挂起,并且图像也全部加载(我可以看到滚动条的尺寸变小了

在询问用户权限后,图像不会立即显示。我必须重新启动应用程序才能查看这些图像。接受许可后,这些图像应该是可见的。

This is the normal look of my app  Sometime when scrolling some white space also come between the images.

1这是我的应用的正常外观。

2有时在图像之间也会滚动一些空白(以棕色椭圆形显示)。

为了减少内存,我试图降低imageView的高度(认为现在可能要滑行才能加载更少的像素),但根本不起作用。 对于用户权限,我尝试使用《 Android开发人员指南》,但还是没有运气。

// this is my main activity
    private static final int MY_PERMISSIONS_REQUEST = 100;
    private Cursor mCursor=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // layout initialization code
        mCursor = getImageCursor();
        requestStoragePermission();

        recyclerView = findViewById(R.id.imageRecyclerView);
        mAdapter = new mayankImageAdapter(this,mCursor);

      //..... (setting the recyclerView)
        RecyclerView.LayoutManager mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

    }

    private void requestStoragePermission() {
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // file-related task you need to do.

                }
                else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
        }
    }


    public Cursor getImageCursor(){
        try{
            Uri externalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] projection = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.TITLE,
                            MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATA};
            String sortOrder = MediaStore.Images.Media.TITLE+" ASC";

            Cursor imageCursor = getContentResolver().query(externalUri,
                        projection,null,null,sortOrder);
            return imageCursor;
        }catch(Exception e){
            return null;
        }
    }
    @Override
    protected void onDestroy() {
        mCursor.close();
        super.onDestroy();
    }
}

mayankImageAdapter.java

\\ onBindViewHolder method
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
        if(imageCursor!=null) {
            imageCursor.moveToFirst();
            imageCursor.moveToPosition(position);
            int strIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            String str = imageCursor.getString(strIndex);
            File myimageloc = new File(str);
            GlideApp.with(callingActivity).load(myimageloc).into(myViewHolder.imgView); 
        }else{
            return;
        }
    }

acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:id="@+id/imageRecyclerView"
    xmlns:android="http://schemas.android.com/apk/res/android" />

我想知道我的应用程序占用这么多内存的原因,以及减少内存的步骤是什么,在询问用户权限后如何显示图像。

谢谢

编辑:我已经更新了用户权限代码和RecyclerView适配器代码。

0 个答案:

没有答案