这是我的代码(在GitHub上检查此文件full code),用于将图像上传到Firebase存储。
// ImagePickerButton shows an image picker to upload a image for a message
mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(i, "Complete action using"), RC_PHOTO_PICKER);
}
});
这是OnActivityResult
方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
//get the reference to stored file at database
StorageReference selectedImageFilePath = mStorageReference.child(selectedImageUri.getLastPathSegment());
//upload file to firebase
selectedImageFilePath.putFile(selectedImageUri).addOnSuccessListener(MainActivity.this, new OnSuccessListener < UploadTask.TaskSnapshot > () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
@SuppressWarnings("VisibleForTests")
String downloadUrl = taskSnapshot.getDownloadUrl().toString();
Message message = new Message(null, userName, downloadUrl);
messagesDatabaseReference.push().setValue(message);
}
});
}
}
}
从图库中选择Image
可以正常工作,但我无法在Firebase存储上传所选图像。纠正我的代码如果我在任何网站上都错了。但是,我在manifest
文件
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我目前在通过USB连接的移动设备上运行应用程序(Moto G4 - Nougat)
答案 0 :(得分:0)
data.getData()
可能会返回不指向文件的URI。
你应该使用
InputStream is = getContentResolver().openInputStream(data.getData())
而不是使用
selectedImageFilePath.putStream(is)
您还可以在上传任务中添加onFailureListener并调试出错的地方。
此外,如果我没记错的话,当您直接阅读onActivityResult
中的数据时,我认为您不需要存储权限。
答案 1 :(得分:0)
尝试按钮点击:
//Select Image functionality
btnselectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 2);
}
});
将图像保存到火场:
//Storing and getting image from firebase
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
final Uri uri = data.getData();
StorageReference filepath = mStorageRef.child("Photos").child(name);
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
//Getting url where image is stored
saveImage(downloadUri);
Picasso.with(getActivity()).load(downloadUri).fit().centerCrop().into(profileImage);
}
});
}
这对我来说很好,希望这会有所帮助。
答案 2 :(得分:0)
我通过编写单独的else if
语句来修改它,而不是if
语句。
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
}else if (resultCode == RESULT_CANCELED) {
}
// This was the change I made
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
}
}
答案 3 :(得分:-1)
If ( //sign in)
{
If (//result ok)
{}
else if (//result cancel)
{}
}
else if (//rc photot picker && result ok)
{ //your storage code here.....}
tats工作得很完美.....仔细检查牙箍....