gpt4 book ai didi

java - 如何在我的第二个 Activity 中将图像上传到 Firebase 存储?

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:58 24 4
gpt4 key购买 nike

请告诉我如何将第一个 Activity 捕获的图像上传到 Firebase。按下发送图像按钮后,图像将进入第二个 Activity 。我可以在 ImageView 中进行设置,但无法将其上传到 Firebase 存储中。请告诉我哪里错了。

这是我的第一个 Activity

  //camera
camera=(ImageView)findViewById(R.id.cam);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent,CAMERA_REQUEST_CODE);
// Intent intent = new Intent(Home.this,PostActivity.class);
// startActivity(intent);
}
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==CAMERA_REQUEST_CODE)

{
Uri uri=data.getData();
Intent intent=new Intent(Home.this,PostActivity.class);
intent.putExtra("imgUrl",uri.toString() );
startActivity(intent);


}
}

这是我的第二项 Activity

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
// path = (Uri) bundle.get("imgUrl");
path = Uri.parse(bundle.getString("imgUrl"));
Log.e("ashish", path + "");

}

ImageView selfiiii = (ImageView) findViewById(R.id.mySelfie);
selfiiii.setImageURI(path);



btnPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {



startPosting();
}
});
}

public void startPosting() {

dialog.setMessage("posting....");
dialog.show();
final String status = WriteSomthng.getText().toString().trim();


if (!TextUtils.isEmpty(status) && path!=null) {

StorageReference filpath = reference.child("Posts").child(path.getLastPathSegment());
Log.e("irfan sam",filpath+"");
filpath.putFile(path).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference userPost = database.push();
userPost.child("status").setValue(status);
userPost.child("image").setValue(downloadUrl.toString());
userPost.child("userName").setValue(Common.currentUser.getUserName());

Intent intent = new Intent(PostActivity.this, Home.class);
startActivity(intent);
Toast.makeText(PostActivity.this, "Posted", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});


}
}

最佳答案

您需要从 putFile(..) 方法的图像路径创建一个File。查看 Firebase 的以下官方示例。

    // File or Blob
file = Uri.fromFile(new File("path/to/mountains.jpg"));

// Create the file metadata
metadata = new StorageMetadata.Builder()
.setContentType("image/jpeg")
.build();

// Upload file and metadata to the path 'images/mountains.jpg'
uploadTask = storageRef.child("images/"+file.getLastPathSegment()).putFile(file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
}
});

关于java - 如何在我的第二个 Activity 中将图像上传到 Firebase 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48880052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com