【发布时间】:2019-08-16 10:09:36
【问题描述】:
我正在尝试在将文件上传到 firebese 之前对其进行压缩。
不幸的是,没有任何事情发生:无法解码流: java.io.FileNotFoundException: /com.marijan.red.MessageActivity@dac48b3/document/image:256(没有这样的 文件或目录)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICK && resultCode == RESULT_OK){
Uri imageUri = data.getData();
DatabaseReference user_message_push = reference.child("Chats")
.child(fuser.getUid()).child(userid).push();
final String push_id = user_message_push.getKey();
final StorageReference filePath = mImageStorage.child("message_images").child(push_id +"jpg");
File actualImage = new File (imageUri.getPath());
try {
Bitmap compressedImage = new Compressor(this)
.setQuality(50)
.compressToBitmap(actualImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImage.compress(CompressFormat.JPEG, 50, baos);
byte[] final_image = baos.toByteArray();
UploadTask uploadTask = filePath.putBytes(final_image);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
String download_url = task.getResult().toString();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", fuser.getUid());
hashMap.put("receiver",userid );
hashMap.put("message", download_url);
hashMap.put("isseen", false);
hashMap.put("type","image");
reference.push().setValue(hashMap);
我假设我没有正确传递 ImageUri
【问题讨论】:
-
检查是否在运行时询问 READ_PERMISSION (SDK >= 23) & manifest
-
您的
imageUri可以,但imageUri.getPath()不行-它没有指向任何有效路径 -
我该怎么做?
标签: android image compression chat