RecycleView中的毕加索未加载图像(可能是路径错误)

时间:2019-03-18 07:48:06

标签: java android android-recyclerview picasso

大家好,我在使用Picasso将图像加载到Recycleview时遇到问题。我想将图像路径保存到db并在recycleview上显示图像。当我手动使用路径时,它可以工作,但是我认为它无法正确保存或检索。

selectedImage重新调整为-content://com.android.providers.media.documents/document/image%3A392

getImage重新调整为-/document/image:392

并另存为-/document/image:392

从适配器:

public void onBindViewHolder(MyHolder holder, int position) {

    holder.notestxt.setText(notes.get(position).getNote());
    if (notes.get(position).getImage().trim().length() != 0) {
        holder.img1.setVisibility(VISIBLE);
        Picasso.get().load(notes.get(position).getImage()).into(holder.img1);
    } else {
        holder.img1.setVisibility(GONE);
    }

    holder.setItemClickListener(new ItemClickListener() {

        @Override
        public void onItemClick(View v, int pos) {
            //OPEN DETAIL ACTIVITY
            //PASS DATA

            //CREATE INTENT
            Intent i = new Intent(c, editnote.class);

            //LOAD DATA
            i.putExtra("NOTE", notes.get(pos).getNote());

            //START ACTIVITY
            c.startActivity(i);
        }
    });
}

活动的一部分:

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;
        case R.id.action_image:
            ActivityCompat.requestPermissions(addnote.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);
            getimages();
            break;
        case R.id.action_save:
            if (editable_item.getText().toString().trim().length() != 0) {
                save(editable_item.getText().toString(), "");
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                Toast.makeText(getApplicationContext(), "Note saved", Toast.LENGTH_SHORT).show();
            } else {
                String path = selectedImage.getPath();
                save("", path);
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                Toast.makeText(getApplicationContext(), "Image saved", Toast.LENGTH_SHORT).show();
            }
            break;

        case R.id.action_cancel:
            Intent x = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(x);
            finish();
    }
    return true;
}

private void save(String note, String image) {
    DBAdapter db = new DBAdapter(this);

    //OPEN
    db.openDB();

    //INSERT
    long result = db.add(note, image);

    editable_item.setText("");

    //CLOSE
    db.close();

}

private void getimages() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);

    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {

            selectedImage = data.getData();

            editable_item.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
            Picasso.get().load(selectedImage).into(imageView);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

首先需要通过此代码将data.getData从Uri转换为真实路径

private String getRealPathFromContentUri(Context context, Uri contentURI) {
    String result = null;
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
      try {
        result = contentURI.getPath();
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else {
      cursor.moveToFirst();
      int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
      try {
        result = cursor.getString(idx);
        cursor.close();
      } catch (IllegalStateException e) {
        e.printStackTrace();
        return null;
      }
    }
    return result;
}

您的OnActivityResult如下所示:

@Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {

            selectedImage = getRealPathFromContentUri(context,data.getData());

            editable_item.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
            Picasso.get().load(selectedImage).into(imageView);
        }
}