任何人都可以帮助将选择的图像转换为位图,然后将base64转换为String以便将图像保存在Firebase存储中。如何将所选的图像转换为位图,然后转换为base64并将此字符串上传到Firebase存储。
任何人都可以帮助将所选图像转换为位图,然后将base64转换为String以便将图像保存在Firebase存储中。如何将所选图像转换为位图,然后转换为base64并将此字符串上传到Firebase存储。帮我任何人转换下面的代码以上传图片..........
public class PostActivity extends AppCompatActivity {
Uri imageUri;
String imageUrl;
StorageTask uploadTask;
StorageReference storageReference;
EditText description;
ImageView close,image_added;
TextView post;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
close=findViewById(R.id.close);
description=findViewById(R.id.description);
image_added=findViewById(R.id.image_added);
post=findViewById(R.id.post);
storageReference= FirebaseStorage.getInstance().getReference("posts");
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(PostActivity.this,MainActivity.class));
finish();
}
});
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
CropImage.activity().setAspectRatio(1,1).start(PostActivity.this);
}
private String getFileExtension(Uri uri){
ContentResolver contentResolver=getContentResolver();
MimeTypeMap mime=MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(contentResolver.getType(uri));
}
private void uploadImage (){
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setMessage("Posting...");
progressDialog.show();
if (imageUri !=null){
final StorageReference referencefile=storageReference.child(System.currentTimeMillis()+"."
+ getFileExtension(imageUri) );
uploadTask = referencefile.putFile(imageUri);
uploadTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if(!task.isSuccessful()){
throw task.getException();
}
return referencefile.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task <Uri>task) {
if(task.isSuccessful()){
Uri downloadUri=task.getResult();
imageUrl=downloadUri.toString();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");
String postid = reference.push().getKey();
HashMap <String,Object>hashMap=new HashMap<>();
hashMap.put("postid",postid);
hashMap.put("postimage",imageUrl);
hashMap.put("description",description.getText().toString());
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.child(postid).setValue(hashMap);
progressDialog.dismiss();
startActivity(new Intent(PostActivity.this,MainActivity.class));
finish();
}else {
Toast.makeText(PostActivity.this,"Failed",Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(PostActivity.this,""+e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}else {
Toast.makeText(this,"No Image Selected",Toast.LENGTH_SHORT).show();
}
}
@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) {
imageUri=result.getUri();
image_added.setImageURI(imageUri);
}
}else {
Toast.makeText(this,"Something gone wrong!",Toast.LENGTH_SHORT).show();
startActivity(new Intent(PostActivity.this,MainActivity.class));
finish();
}
}
}