下载图像并将其存储在SD卡上不起作用。我不知道我哪里出错了。
此代码用于将图像上传到Firebase存储。
// Get a reference to store file at photos/<FILENAME>
StorageReference photoRef = mPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
//Upload file to Firebase Storage
photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener < UploadTask.TaskSnapshot > () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.d("TAG", "in Success method of Uploading file");
// When the image has successfully uploaded, we get its download URL
@SuppressWarnings("VisibleForTests")
Uri downloadUrl = taskSnapshot.getDownloadUrl();
// Set the download URL to the message box, so that the user can send it to the database
Message message = new Message(null, userName, downloadUrl.toString());
messagesDatabaseReference.push().setValue(message);
mProgressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Upload Successful", Toast.LENGTH_SHORT).show();
}
});
请注意,我使用以下方法创建本地图像文件:
File localFile = null;
try {
localFile = File.createTempFile("images", "jpg", getExternalFilesDir(null));
Log.d("TAG", "local file: " + localFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
在这里,我使用相同的photoRef
来下载用户上传的特定图片。这是一种正确的做法吗?
photoRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener < FileDownloadTask.TaskSnapshot > () {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Local temp file has been created
Log.d("TAG", "Entered onSuccess in Download File*");
Toast.makeText(MainActivity.this, "Download Success", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Log.d("TAG", "Entered onFailure in Download File*");
Toast.makeText(MainActivity.this, "Download Failed", Toast.LENGTH_SHORT).show();
}
});
我记录了photoRef
和local file
的值。这显示在我的 LogCat 。
Photoref: gs://wecare-8a15d.appspot.com/photos/image:27410
local file: /storage/emulated/0/Android/data/com.pc.wecare/files/images2116155570jpg
这是我在使用exception.getMessage();
时获得的异常Excecption message: Object does not exist at location.
这是来自 LogCat 的详细信息。它显示从图库中提取照片并将其上传到Firebase存储:
05-03 11:25:11.600 8577-8577/com.pc.wecare D/TAG: In Main Activity Pause Method
05-03 11:25:20.277 8577-8577/com.pc.wecare D/TAG: Entered OnActivity photo picker method
05-03 11:25:20.277 8577-8577/com.pc.wecare D/TAG: onActivityResult, requestCode: 2, resultCode: -1
05-03 11:25:20.278 8577-8577/com.pc.wecare D/TAG: onActivityResult, RC_PHOTO_PICKER: 2, RESULT_OK: -1
05-03 11:25:20.310 8577-8577/com.pc.wecare D/TAG: local file: /storage/emulated/0/Android/data/com.pc.wecare/files/images306346474jpg
05-03 11:25:20.310 8577-8577/com.pc.wecare D/TAG: Photoref: gs://wecare-8a15d.appspot.com/photos/image:27301
05-03 11:25:20.313 8577-8577/com.pc.wecare D/TAG: In Main Activity Resume Method
05-03 12:58:14.730 31570-31570/com.pc.wecare D/TAG: Entered onFailure in Download File*
05-03 12:58:17.234 31570-31570/com.pc.wecare D/TAG: in Success method of Uploading file
答案 0 :(得分:0)
请在清单中查看您的许可
File.createTempFile(fileName,null,context.getCacheDir());
这不是文件扩展名
请检查您的文件路径
/storage/emulated/0/Android/data/com.pc.wecare/files/的 images2116155570jpg 强> images211615557的 0.jpg 强>