java - java.lang.String android.net.Uri.toString() 在空对象引用上,LogCat 在第 202 行显示此错误
问题描述
我试图根据存储引用的图像路径是否为空的条件来获取下载 URL。
我试图将整个代码放在 if else 语句中,而不是在解决错误的条件内将值分配给 download_uri。我无法理解为什么这种方法有效而另一种方法无效。
mSaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name=mProfileName.getText().toString();
if(!TextUtils.isEmpty(name) && mainImageUri!=null){
mProgressBar.setVisibility(View.VISIBLE);
if(isChanged){
//The task of storing the data goes through uploading the image as well.
user_id = mUser.getUid();
final StorageReference image_path= mStorageReference.child("profile_photo").child(user_id+".jpg");
image_path.putFile(mainImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
storeFireStore(image_path,name);
}else{
String error = task.getException().getMessage();
Toast.makeText(SetUpActivity.this,"Image Error: "+error,Toast.LENGTH_SHORT).show();
}
mProgressBar.setVisibility(View.INVISIBLE);
}
});
}else{
//The task of storing the data does not go through uploading the image.
storeFireStore(null,name);
mProgressBar.setVisibility(View.INVISIBLE);
}
}
}
});
}
private void storeFireStore(StorageReference image_path, final String name) {
final Uri[] download_uri = new Uri[1];
if(image_path!=null){
image_path.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
download_uri[0] =uri;
Log.d(TAG,"Download Url obtained");
}
});
} else{
download_uri[0] =mainImageUri;
Log.d(TAG,"Download Url obtained in else");
}
Map<String,String> userMap=new HashMap<>();
userMap.put("image", download_uri[0].toString());//line 202 this is
where the error occurs.userMap.put("name",name);
我希望得到 downLoad_uri 但它提供了一个空值
解决方案
如果您可以通过简单的在线代码来完成,我不知道您为什么要做不必要的工作来获取图像的下载 URL。
只需使用以下代码将图像上传到 Firebase 存储。
image_path.putFile(mainImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
// HERE YOU CAN DIRECTLY GET DOWNLOAD URL OF UPLOADED IMAGE USING TASK RESULT.
String downloadUrl = String.valueOf(taskSnapshot.getDownloadUrl());
storeFireStore(downloadUrl,name);
} else {
String error = task.getException().getMessage();
Toast.makeText(SetUpActivity.this,"Image Error: "+error,Toast.LENGTH_SHORT).show();
}
mProgressBar.setVisibility(View.INVISIBLE);
}
});
并改变storeFireStore
如下方法,
private void storeFireStore(String image_path, final String name) {
Map<String, String> userMap = new HashMap<>();
userMap.put("image", image_path != null ? image_path : "");//line 202 this is where the error occurs.
userMap.put("name", name);
}
推荐阅读
- r - Shiny - 如何根据原始数据第一个表中的用户输入更新 Shiny 中的第二个汇总表?
- bash - 尝试管道输出时出现 Bash 错误:语法错误
- java - J8 流按列表中的向量排序
> - python - 将嵌套的 JSON 展平为 pandas.DataFrame:基于字典值的排序和命名列
- python - Python 分类模型
- three.js - Three.JS - 补间相机位置
- android - RecyclerView - notifyDataSetChanged() - Kotlin - 不工作
- spring-cloud-stream - 将 Spring Cloud Sleuth 与 OpenZipkin 一起使用。GUI 未显示所有跨度
- audio - 处理音频点击/弹出声音的算法
- css - 我正在尝试对具有最大和最小宽度的背景颜色应用媒体查询,但浏览器在不同维度上应用查询