有效地从数据库加载所有行(Content Provider,RecyclerView)

时间:2018-02-05 21:11:34

标签: java android database

我是Android Developemnt的新手。目前,我正在开发某种有趣的图像管理器" (只是简单的应用程序练习),我创建了简单的DB存储: - 文件名 -path到文件(我不想将图像存储为BLOB,因为图像可能非常大) - 一些额外的信息 我还有一个片段显示所有图像(它加载所有行) - 它是一个RecyclerView。

目前我做了类似的事情: 片段使用contentResolver(另外我有contentProvider)从DB获取所有数据,如下所示:

private void fetchDataFromDb(){


    //uri for  query for the  whole Table 
    Uri uri= DatabaseDescription.Picture.CONTENT_URI;

    //query for ContentResolver
    //TODO warning: passing null as projection is unefficient - returns all columns ( from docs -> I want that though)

    Cursor cursor=contentResolver.query(uri,null,null,null,null,null);

    while(cursor.moveToNext()){
        // my strange fetching from cursor
        String path=cursor.getString(cursor.getColumnIndex(DatabaseDescription.Picture.COLUMN_PIC_PATH));
        String name=cursor.getString(cursor.getColumnIndex(DatabaseDescription.Picture.COLUMN_PIC_NAME));
        String fileName=cursor.getString(cursor.getColumnIndex(DatabaseDescription.Picture.COLUMN_FNAME));
        String description=cursor.getString(cursor.getColumnIndex(DatabaseDescription.Picture.COLUMN_DESCRIPTION));

        funnyImagesList.add(new MyFunnyImg(path,name,description,fileName));
    }
}

MyFunnyImg是一个描述图像的类 - 它包含名称,路径等 funnyImagesList是一个List(ArrayList),它存储所有图像(作为MyFunnyImg类的对象),并传递给RecyclerViewAdapter,以便在onBindViewHolder方法中加载正确的图像(使用MyFunnyImg类的方法 - 下面的方法)

onBindViewHolder方法:

 public void onBindViewHolder(RecycleAdapterImage.ViewHolder holder, int position) {

    String description=argsToShow.get(position).getDescription();
    Bitmap image=argsToShow.get(position).loadBitmap(activity);
    String name=argsToShow.get(position).getName();



    CardView cardView=holder.cardView;
    ImageView imageView=(ImageView)cardView.findViewById(R.id.picture);
    imageView.setImageBitmap(image);
    TextView textView=(TextView)cardView.findViewById(R.id.card_view_des);
    textView.setText(description);//description
}

来自MyFunnyImg的加载方法:

public Bitmap loadBitmap(Activity activity){

    //String root= Environment.getExternalStorageDirectory().toString();


    //TODO change this strange structure
    String root=null;
    try {
         root = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString();
    }catch(NullPointerException ex){
        ex.printStackTrace();
        Log.e("Error","NullPointerException while loeadin BitMap");
    }




    File dir=new File(root+"/saved_images");

    Bitmap bitmap=null;

    Log.i("Loading",fileName);
    File file=new File(dir, fileName);

    if (activity.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        Log.i("Info","Asking for permission to read if not granted yet");
        ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},4200);
    }else {

        try {
            FileInputStream in = new FileInputStream(file);
            bitmap = BitmapFactory.decodeStream(in);
            if(bitmap==null)Toast.makeText(activity,"In RecyclerView bitmap is null1",Toast.LENGTH_SHORT).show();

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

            Log.e("Error:", "Error while reading an image");
        }
    }

    if(bitmap==null)Toast.makeText(activity,"In RecyclerView bitmap is null2",Toast.LENGTH_SHORT).show();
    return bitmap;
}

我知道可能会有一些可怕的"建筑但我稍后会解决它们。我的问题是 - 如何更有效地做同样的事情?我正在阅读背景中的线程,所以一个(也许我的代码中有一些但我不知道它?)并且想尽可能高效地工作(现在我觉得我的应用程序是当我输入这个片段时会滞后 - 即使那里有2/3个图像......)。

0 个答案:

没有答案