Я работаю над этим приложением для блога с firebase в бэкэнде, но оно не получает изображение. На самом деле это не показывает изображения ни изображение профиля, ни изображение блога. Я вставил код приложения, помогите мне в получении изображения. Заранее спасибо.
'' package com.example.theblog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.theblog.BlogPost;
import com.example.theblog.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import java.sql.Date;
import java.text.DateFormat;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {
public List<BlogPost> blog_list;
public Context context;
private FirebaseFirestore firebaseFirestore;
public FirebaseAuth firebaseAuth;
public BlogRecyclerAdapter(List<BlogPost> blog_list){
this.blog_list=blog_list;
}
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item,parent,false);
context=parent.getContext();
firebaseAuth=FirebaseAuth.getInstance();
firebaseFirestore=FirebaseFirestore.getInstance();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
holder.setIsRecyclable(false);
String desc_data=blog_list.get(position).getDesc();
holder.setDescText(desc_data);
String imageUrl=blog_list.get(position).getImage_url();
holder.setBlogImage(imageUrl);
try{
long millisecond=blog_list.get(position).getTimestamp().getTime();
android.text.format.DateFormat df = new android.text.format.DateFormat();
String dateString=df.format("dd/MM/yyyy ", new Date(millisecond)).toString();
holder.setDate(dateString);
}catch (Exception e){
Toast.makeText(context,"Exception"+e.getMessage(),Toast.LENGTH_LONG).show();
}
String user_id=blog_list.get(position).getUser_id();
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(
new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
String userName=task.getResult().getString("name");
String userImage=task.getResult().getString("image");
holder.setUserData(userName,userImage);
}else {
String error=task.getException().getMessage();
Toast.makeText(context,"Error"+error,Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public int getItemCount() {
return blog_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private TextView descView;
private TextView blogDate;
private CircleImageView blogUserImage;
private ImageView blogImage;
private TextView blogUserName;
public ViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setDescText(String descText){
descView=mView.findViewById(R.id.blog_post_desc);
descView.setText(descText);
}
public void setDate(String date){
blogDate=mView.findViewById(R.id.blog_date);
blogDate.setText(date);
}
public void setUserData(String userName,String userImage){
blogUserName=mView.findViewById(R.id.blog_user_name);
blogUserName.setText(userName);
blogUserImage=mView.findViewById(R.id.blog_user_image);
RequestOptions request=new RequestOptions();
request.placeholder(R.color.grayColor);
Glide.with(context).applyDefaultRequestOptions(request).load(userImage).into(blogUserImage);
}
public void setBlogImage(String downloadUri){
blogImage=mView.findViewById(R.id.blog_image);
RequestOptions requestOptions=new RequestOptions();
requestOptions.placeholder(R.color.grayColor);
Glide.with(context).load(downloadUri).into(blogImage);
}
}
}
'' 'Код дляBlogPost приведен ниже.
'' '
package com.example.theblog;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.util.HashMap;
import java.util.Map;
import de.hdodenhof.circleimageview.CircleImageView;
public class PostActivity extends AppCompatActivity {
private ImageView newPostImage;
private EditText newPostDesc;
private FloatingActionButton newPostBtn;
private Uri postImageUri=null;
private StorageReference storageReference;
private FirebaseFirestore firebaseFirestore;
private FirebaseAuth firebaseAuth;
private ProgressBar progressBar;
private String current_user_id;
private static final int GalleryPick =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
setTitle("Post Blog");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
storageReference= FirebaseStorage.getInstance().getReference();
firebaseAuth=FirebaseAuth.getInstance();
firebaseFirestore=FirebaseFirestore.getInstance();
current_user_id=firebaseAuth.getCurrentUser().getUid();
newPostImage=findViewById(R.id.image);
newPostBtn=findViewById(R.id.btn);
newPostDesc=findViewById(R.id.desc);
progressBar=findViewById(R.id.progress1);
newPostImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(PostActivity.this);
}
});
newPostBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String desc=newPostDesc.getText().toString();
if (!TextUtils.isEmpty(desc) && postImageUri!=null){
String randomName= FieldValue.serverTimestamp().toString();
progressBar.setVisibility(View.VISIBLE);
StorageReference filepath=storageReference.child("post Images").child(randomName+".jpg");
filepath.putFile(postImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
Task<Uri> downloadUri=task.getResult().getMetadata().getReference().getDownloadUrl();
Map<String,Object> postMap=new HashMap<>();
postMap.put("image_url",downloadUri.toString());
postMap.put("user_id",current_user_id);
postMap.put("desc",desc);
postMap.put("timestamp",FieldValue.serverTimestamp());
firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (task.isSuccessful()){
Toast.makeText(PostActivity.this,"Your Post is Uploaded",Toast.LENGTH_LONG).show();
Intent mainIntent=new Intent(PostActivity.this,MainActivity.class);
startActivity(mainIntent);
}else {
String error =task.getException().getMessage();
Toast.makeText(PostActivity.this,"Error"+error,Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.INVISIBLE);
}
}
});
}else {
String error =task.getException().getMessage();
Toast.makeText(PostActivity.this,"Error"+error,Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.INVISIBLE);
}
}
});
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result =CropImage.getActivityResult(data);
if (resultCode==RESULT_OK){
postImageUri=result.getUri();
newPostImage.setImageURI(postImageUri);}
else if (resultCode==CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE){
Exception error = result.getError();
}
}
}
}
' ''
Пожалуйста, помогите мне найти решение этой проблемы и заранее спасибо.