当我在Firebase中上传图片时,它会正确上传。图片目录中存储的网址为“ com.google.android.gms.tasks.zzu@de15f74”,该网址无效。因此,我无法检索到正确的图片使用毕加索在图像视图中设置图像的URL ................................................. ................................................... ................................................... ...............................
java
package com.example.textme;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.mikhaellopez.circularimageview.CircularImageView;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import id.zelory.compressor.Compressor;
import static android.view.View.*;
public class SettingsActivity extends AppCompatActivity {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
private CircularImageView mDisplayImage;
private TextView mName;
private TextView mStatus;
private Button mStatusBtn;
private Button ImageBtn;
private Button mImageBtn;
private static final int GALLERY_PICK = 1;
private StorageReference mImageStorage;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mDisplayImage = (CircularImageView)findViewById(R.id.settings_image);
mName = (TextView)findViewById(R.id.settings_display_name);
mStatus = (TextView)findViewById(R.id.settings_status);
mStatusBtn = (Button)findViewById(R.id.settings_status_btn);
mImageBtn = (Button)findViewById(R.id.settings_image_btn);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = mCurrentUser.getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mImageStorage = FirebaseStorage.getInstance().getReference();
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
final String image = dataSnapshot.child("image").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();
mName.setText(name);
mStatus.setText(status);
if(!image.equals("default"))
Picasso.get().load(image).placeholder(R.drawable.avatar).into(mDisplayImage);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
mStatusBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String status_value = mStatus.getText().toString();
Intent status_intent = new Intent(SettingsActivity.this,StatusActivity.class);
status_intent.putExtra("status_value",status_value);
startActivity(status_intent);
}
});
mImageBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent gallery_Intent = new Intent();
gallery_Intent.setType("image/*");
gallery_Intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gallery_Intent,"SELECT IMAGE"), GALLERY_PICK);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_PICK && resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
CropImage.activity(imageUri)
.setAspectRatio(1,1)
.setMinCropWindowSize(500,500)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mProgressDialog = new ProgressDialog(SettingsActivity.this);
mProgressDialog.setTitle("Uploading Image...");
mProgressDialog.setMessage("Please wait while we upload and process the image");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
Uri resultUri = result.getUri();
final File thumb_filePath = new File(resultUri.getPath());
String current_user_id = mCurrentUser.getUid();
Bitmap thumb_bitmap = null;
try {
thumb_bitmap = new Compressor(this)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_filePath);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
final StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id+".jpg");
final StorageReference thumb_filepath = mImageStorage.child("profile_images").child("thumbs").child(current_user_id+".jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful())
{
final String download_url = task.getResult().getStorage().getDownloadUrl().toString();
UploadTask uploadTask = thumb_filepath.putBytes(thumb_byte);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {
String thumb_download_url = thumb_task.getResult().getStorage().getDownloadUrl().toString();
if(thumb_task.isSuccessful())
{
Map update_hashMap = new HashMap<>();
update_hashMap.put("image",download_url);
update_hashMap.put("thumb_image",thumb_download_url);
mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
mProgressDialog.dismiss();
}
}
});
}
else
{
Toast.makeText(SettingsActivity.this,"Error in uploading thumbnail",Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}
});
}
else
{
Toast.makeText(SettingsActivity.this,"Error in uploading",Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}
});
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}