请告诉我如何将图片上传到第一项活动中捕获的Firebase
。发送图像按钮后按下图像进入第二个活动。我可以在ImageView
设置,但无法将其上传到Firebase
存储空间。请告诉我哪里错了。
//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);
}
}
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();
}
});
}
}
答案 0 :(得分:0)
您需要从File
方法的图片路径创建putFile(..)
。请查看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();
}
});
答案 1 :(得分:0)
如果您在Uri
中有图像,则无需使用以下方法进行转换即可存储该图像:
private void uploadImage(Uri file) {
if (file != null) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference ref = storage.getReference().child("images/myPath/");
ref.putFile(file)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(FileUploadPage.this, "Uploaded", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(FileUploadPage.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded "+(int)progress+"%");
}
});
}
}
要从Uri
获取Intent
,您可以使用以下代码:
在您的第一个活动中,您将图片Uri
视为额外内容:intent.putExtra("imgUrl", uri.toString());
然后是第二项活动:
Intent intent = getIntent();
Uri path;
if (intent.hasExtra("imgUrl")) {
path = Uri.fromFile(new File(getIntent().getStringExtra("imgUrl")));'
}
uploadImage(path);
Uri.fromFile(new File(String path)
应该保护您免受错误的Uri
解码。
答案 2 :(得分:0)
由于您有路径,因此您可以创建Uri
并上传
filpath.putFile(Uri.fromFile(new File("/sdcard/cats.jpg"))).addOnSuccessListener(....
此外,如果您有一个大文件,则应创建Uri
异步。
您应该添加FailureListener
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
通过这种方式,您可以查看上传内容的错误。