public class ImageListAdapter extends ArrayAdapter<ImageUpload> {
private Activity context;
private int resource;
private List<ImageUpload> listImage;
public ImageListAdapter(@NonNull Activity context, @LayoutRes int resource,
@NonNull List<ImageUpload> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
listImage = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull
ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View v = inflater.inflate(resource, null);
TextView tvName = v.findViewById(tvImageN);
ImageView img = v.findViewById(R.id.imgView);
tvName.setText(listImage.get(position).getName());
//i think the is in this code, the image dont load in the listview
/ *名称显示在列表视图中,但图像不显示,我尝试更改Firebase规则,但它是相同的 * / Picasso.with(context).load(listImage.get(position).getUrl())。into(img);
return v;
}
}
//这是显示类,它将在其中显示图像和名称 / * 我制定了所有规则,并添加了互联网许可和读写权限,但仍然相同
* /
公共类ImageDsiplay扩展了AppCompatActivity {
private DatabaseReference mDatabaseRef;
private List<ImageUpload> imgList;
private ListView lv;
private ImageListAdapter adapter;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_dsiplay);
imgList = new ArrayList<>();
lv = findViewById(R.id.ListViewImage);
//Show progress dialog during list image loading
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait loading list image...");
progressDialog.show();
mDatabaseRef = FirebaseDatabase.getInstance().getReference(CamerGallery.FB_DATABASE_PATH);
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
progressDialog.dismiss();
//Fetch image data from firebase database
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//ImageUpload class require default constructor
ImageUpload img = snapshot.getValue(ImageUpload.class);
imgList.add(img);
}
//Init adapter
adapter = new ImageListAdapter(ImageDsiplay.this, R.layout.image_item, imgList);
//Set adapter for listview
lv.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
}
//这是将其上传到firebase并将其存储在其中的类
公共类CamerGallery扩展了AppCompatActivity {
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;
private ImageView imageView;
private EditText txtImageName;
private Uri imgUrl;
Context context;
public static final String STORAGE_PATH = "image/";
public static final String FB_DATABASE_PATH = "image";
public static final int REQUEST_CODE = 1234;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camer_gallery);
mStorageRef = FirebaseStorage.getInstance().getReference();
mDatabaseRef = FirebaseDatabase.getInstance().getReference(FB_DATABASE_PATH);
imageView = findViewById(R.id.imageView);
txtImageName = findViewById(R.id.txtImageName);
}
public void btnBrowse_Click(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select image"), REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
imgUrl = data.getData();
try {
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUrl);
imageView.setImageBitmap(bm);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getImageExt(Uri uri) {
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
public void btnUpload_Click(View v) {
if (imgUrl != null) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Uploading image");
dialog.show();
//get the storage refrence
final StorageReference ref = mStorageRef.child(STORAGE_PATH + System.currentTimeMillis() + "." + getImageExt(imgUrl));
//add file to refrence
ref.putFile(imgUrl).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Image Uploaded", Toast.LENGTH_SHORT).show();
ImageUpload imageUpload = new ImageUpload(txtImageName.getText().toString(), ref.getDownloadUrl().toString());
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadId).setValue(imageUpload);
//imageView.setImageResource(0);
//txtImageName.setText("");s
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
dialog.setMessage("Upload " + (int) progress + "%");
}
});
} else {
Toast.makeText(getApplicationContext(), "Please Select Image", Toast.LENGTH_SHORT).show();
}
}
public void btnShowListImage_Click(View v) {
Intent i = new Intent(CamerGallery.this, ImageDsiplay.class);
startActivity(i);
}
}
答案 0 :(得分:0)
在您发表评论后,您变得更清楚了,您的问题是尝试以这种方式ImageUpload img = snapshot.getValue(ImageUpload.class);
构建新的ImageUpload,这是不对的。而是使用以下ImageUpload img = new ImageUpload (snapshot.getKey(), snapshot.getValue().toString());
:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
progressDialog.dismiss();
//Fetch image data from firebase database
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//ImageUpload class require default constructor
ImageUpload img = new ImageUpload (snapshot.getKey(), snapshot.getValue().toString());
imgList.add(img);
}
}
祝你好运!