我正在创建一个回收站视图,以显示用户提供给它的图像,获取图片目录,然后创建它们的文件,然后从我的文件创建位图并将其设置为我的图像视图。但是当用户选择图片时,回收视图不会显示该图片。
我使用了权限和dexter库,但是没有用。
我的适配器
public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {
private Context context;
private List<model> list;
public Adaptor(Context context, List<model> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public Adaptor.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.imageview,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adaptor.ViewHolder viewHolder, int i) {
model model=list.get(i);
Uri uri= Uri.parse(model.getPath());
File file=new File(String.valueOf(uri));
Bitmap bitmap= BitmapFactory.decodeFile(file.getAbsolutePath());
viewHolder.img.setImageBitmap(bitmap);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img=itemView.findViewById(R.id.img_view);
}
}
我的数据库
public class database extends SQLiteOpenHelper {
public database(@Nullable Context context) {
super(context, "add_image", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS ADD_IMAGE(_ID INTEGER PRIMARY KEY AUTOINCREMENT,_PICTURES varchar )");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long insert(String directory){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("_PICTURES",directory);
return db.insert("ADD_IMAGE",null,cv);
}
public Cursor cursor(){
SQLiteDatabase DB=this.getReadableDatabase();
return DB.rawQuery(" select * from ADD_IMAGE",null);
}
}
我的模型班
public class model {
String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
我的活动
public class MainActivity extends AppCompatActivity {
database database=new database(this);
SQLiteDatabase sqLiteDatabase;
RecyclerView rec_view;
List<model> list=new ArrayList<>();
Context context;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dexter.withActivity(this).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE).withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
Toast.makeText(context, "thanks", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(context, "please give premission", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
}
}).check();
context = this;
final Adaptor adaptor = new Adaptor(context, list);
rec_view = findViewById(R.id.rec_image);
rec_view.setAdapter(adaptor);
rec_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
btn = findViewById(R.id.loadimage);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
Cursor cursor = database.cursor();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
model model = new model();
model.setPath(cursor.getString(1));
list.add(model);
adaptor.notifyDataSetChanged();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==RESULT_OK){
Uri tartgeturi=data.getData();
String directory=String.valueOf(tartgeturi);
database.insert(directory);
}
}
}
我希望我的回收站视图正确显示图片,
答案 0 :(得分:0)
Picasso
.with(context)
.load(model.getpath())
.into(viewHolder.img);
从uri到设置图像代码,用此代码替换。但在您的gradle中添加毕加索
implementation 'com.squareup.picasso:picasso:2.5.2'
答案 1 :(得分:0)
您可以通过2种方式来实现 1.给出您下载的图像的本地路径,如下所示
String imgPath = Environment.getExternalStorageDirectory() + "/dir_name"+ fileName;
在科特林
val imgPath = Environment.getExternalStorageDirectory() + "/dir_name"+ fileName;
然后
Glide.with(mContext).load(imgPath).into(profileAvatar);
我用过滑翔机,也可以用毕加索
File myimgFile = new File("path to you image");
Bitmap imgbitmap = BitmapFactory.decodeFile(myimgFile.getAbsolutePath()
view.setImageBitmap(imgbitmap)