我正在编写一个代码,该代码通过recyclerView从Firebase存储下载图像并将其保存在内部存储中,但是无法保存在电话的内部存储中。
download_btn 按钮用于从Firebase下载图像。 storageReference 用于引用文件。 elementAddress 包含图像在recyclerView和图像URL中的位置。
代码如下:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<List_data>list_data;
private Context ct;
ProgressDialog progressDialog;
StorageReference storage;StorageReference fileRef;
Map<Integer,String> elementAddress = new HashMap<Integer, String>() ;
public MyAdapter(List<List_data> list_data, Context ct) {
this.list_data = list_data;
this.ct = ct;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_data,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
storage = FirebaseStorage.getInstance().getReference();
final List_data ld=list_data.get(position);
holder.tvname.setText(ld.getName());
fileRef = storage.child(ld.getImgUrl());
Log.i("CLICKED DATA",Integer.toString(position));
elementAddress.put(position,ld.getImgUrl());
final long ONE_MEGABYTE = 1024*1024*5;
fileRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
holder.imageView.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
@Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
private TextView tvname;
private Button download_btn;
public ViewHolder(View itemView) {
super(itemView);
imageView=(ImageView)itemView.findViewById(R.id.img_view);
tvname=(TextView)itemView.findViewById(R.id.uName);
download_btn = itemView.findViewById(R.id.download_pic_btn);
//download_btn.setTag(123,itemView);
download_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("ITEM CLICKED",Integer.toString(getAdapterPosition()));
StorageReference storageReference = storage.child(elementAddress.get(getAdapterPosition()));
long ONE_MEGABYTE = 1024*1024*5;
storageReference.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
ContextWrapper cw = new ContextWrapper(ct.getApplicationContext());
File directory = cw.getDir("imageDir",Context.MODE_PRIVATE);
if(!directory.exists()){
directory.mkdir();
}
File myPath = new File(directory,"image"+".jpg");
FileOutputStream fos=null;
try{
fos = new FileOutputStream(myPath.getAbsolutePath());
Log.i("PATH OF FILE", myPath.getAbsolutePath().toString());
bitmap.compress(Bitmap.CompressFormat.PNG,100,fos);
}catch (Exception e){
Log.i("ERROR WHILE UPLOADING",e.getMessage());
e.printStackTrace();
}finally {
try {
fos.flush();
fos.close();
Toast.makeText(ct.getApplicationContext(),"FILE DOWNLOADED",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
});
}
}
}