我的应用需要将图片上传到firebase存储并将其显示在recyclerView中。我的前两张图片在列表中显示得很好。但是,当我尝试上传第三张图片时,应用程序崩溃,也无法将图像存储在firebase上。每个图像大小从500KB到700KB不等。我也在我的清单中使用了android:largeHeap =“true”。怎么办?
//这是代码:
{ //Array at 0th index
date = "16 December 2016";
day = Friday;
doctorName = "<null>";
localTime = "07:30 AM";
symptoms = q;
today = Yes;
},
{ //Array at 1st index
date = "16 December 2016";
day = Friday;
doctorName = "<null>";
localTime = "07:30 AM";
symptoms = g;
today = Yes;
}
}
//发布后发生的事情是SUBMITED。
NSArray *allKeys;
for (int i=0; i<[tempDict count]; i++) {
allKeys = [[[tempDict valueForKey:@"upcomingConsultations"] objectAtIndex:i] allKeys];
NSString *targetKey = nil;
// NSArray *allKeys = [[tempDict valueForKeyPath:@"pastConsultations"] allKeys];
for (int j = 0; j < [allKeys count]; ++j) {
NSString *key = [allKeys objectAtIndex:i];
NSString *obj = [[[tempDict valueForKey:@"upcomingConsultations"] objectAtIndex:i] objectForKey:key];
if ([obj rangeOfString:searchText].location != NSNotFound) { // searchedString is what you're looking for
targetKey = key;
NSLog(@"found match");
break;
}
}
}
答案 0 :(得分:2)
尝试以下代码,我使用此代码将图片上传到firebase存储空间,如果您发现任何问题或有任何疑问,请告诉我
1 - 在您的Java文件中,输入这些代码
private SimpleDraweeView imgProfile;
private FirebaseStorage storage;
private Uri file, resultUri;
private StorageMetadata metadata;
private UploadTask uploadTask;
private StorageReference storageRef;
private final String MY_BUCKET_PATH = "YOUR BUCKET PATH OF YOUR STORAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
storage = FirebaseStorage.getInstance();
storageRef = storage.getReferenceFromUrl(MY_BUCKET_PATH);
}
2 - 在onActivityResult中将图片的Uri上传到firebase存储后,还要确保获得正确的图像Uri
private void UploadImage() {
file = Uri.fromFile(new File(resultUri.getPath()));
// Create the file metadata
metadata = new StorageMetadata.Builder().setContentType("image/jpg").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();
showUploadProgress(progress);
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) {
Log.e("Uploading image==", "onSuccess");
// Handle successful uploads on complete
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
imgProfile.setImageURI(Uri.parse(downloadUrl.toString())); // Here image will be reflected after uploading
mDialog.dismiss();
Log.e("DownloadUrl getPath==>>", taskSnapshot.getDownloadUrl().getPath());
Log.e("Metadata getPath==>>", taskSnapshot.getMetadata().getPath()); // this line will give you path to download Image from Firebase storage omitting child storage reference
Log.e("Metadata dwndUrl getpath", taskSnapshot.getMetadata().getDownloadUrl().getPath());
Log.e("storageRef path==>>", taskSnapshot.getMetadata().getReference().getPath());
mSessionManager.storeStringValues("profile_photo", downloadUrl.toString());
mSessionManager.storeStringValues("profile_photo_path", taskSnapshot.getMetadata().getPath());
}
});
}