从firebase存储下载图片失败:android [英] Downloading an Image from firebase storage fails: android

查看:174
本文介绍了从firebase存储下载图片失败:android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下载图像并将其存储在SD卡上不起作用。我不知道我要去哪里错。



此代码用于将图片上传到Firebase存储。

  //获取照片上存储文件的引用/< FILENAME> 
StorageReference photoRef = mPhotosStorageReference.child(selectedImageUri.getLastPathSegment());

//将文件上传到Firebase存储
photoRef.putFile(selectedImageUri).addOnSuccessListener(this,new OnSuccessListener< UploadTask.TaskSnapshot>(){
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot){
Log.d(TAG,成功上传文件的方法);
//当图片成功上传时,下载URL
@SuppressWarnings(VisibleForTests)
Uri downloadUrl = taskSnapshot.getDownloadUrl();
//将下载URL设置为消息框,以便用户可以将它发送给数据库
Message message = new Message(null,userName,downloadUrl.toString());
messagesDatabaseReference.push()。setValue(message);

mProgressBar.setVisibility .GONE);
Toast.makeText(MainActivity.this,Upload Successful,Toast.LENGTH_SHORT).show();
}
});

请注意,我使用以下方法创建本地图像文件:

 文件localFile = null; 
try {
localFile = File.createTempFile(images,jpg,getExternalFilesDir(null));
Log.d(TAG,local file:+ localFile.getAbsolutePath());
} catch(IOException e){
e.printStackTrace();

$ / code>

这里我采用了相同的 photoRef 来下载用户上传的特定图片。这是一个正确的方式吗?

  photoRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener< FileDownloadTask.TaskSnapshot> (){
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot){
//创建了本地临时文件
Log.d(TAG,Entered onSuccess在下载文件*);
Toast.makeText(MainActivity.this,Download Success,Toast.LENGTH_SHORT).show();
}
})。addOnFailureListener(new OnFailureListener ){
@Override
public void onFailure(@NonNull异常异常){
//处理任何错误
Log.d(TAG,Entered onFailure in Download File * );
Toast.makeText(MainActivity.this,Download Failed,Toast.LENGTH_SHORT).show();
}
});

我记录了 photoRef 本地文件。这显示在我的 LogCat 上。

  Photoref:gs://wecare-8a15d.appspot.com / photos / image:27410 
本地文件:/storage/emulated/0/Android/data/com.pc.wecare/files/images2116155570jpg

这是我在使用exception.getMessage()时得到的异常;

  Excecption消息:对象在位置不存在。 

这是 LogCat 的详细信息。它显示从从图库中提取照片并将其上传到Firebase存储:

  05-03 11:25:11.600 8577- 8577 / com.pc.wecare D / TAG:在主要活动中暂停方法
05-03 11:25:20.277 8577-8577 / com.pc.wecare D / TAG:输入OnActivity照片选取器方法
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:本地文件:/ 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 /主要活动简历方法
05-03 12:58:14.730 31570-31570 / com.pc.wecare D / TAG:输入失败下载文件*
05-03 12:58:17.234 31570-31570 / com.pc.wecare D / TAG:in Success meth上传文件


解决方案



  1. File.createTempFile(fileName,null,context.getCacheDir());


这不是文件扩展名

请检查您的文件路径

/storage/emulated/0/Android/data/com.pc.wecare/files/ images2116155570jpg
images211615557 0.jpg


Downloading the image and storing it on SD card doesn't work. I don't know where I'm going wrong.

This code is for uploading an Image to Firebase Storage.

// 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();
  }
});

Note that, I used the following for creating the local image file:

File localFile = null;
    try {
      localFile = File.createTempFile("images", "jpg", getExternalFilesDir(null));
      Log.d("TAG", "local file: " + localFile.getAbsolutePath());
    } catch (IOException e) {
      e.printStackTrace();
    }

Here I'm taking the same photoRef to download that particular image the user uploads. Is this a right way of doing?

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();
      }
    });

I logged the value of photoRef and local file. This showed on my LogCat.

Photoref: gs://wecare-8a15d.appspot.com/photos/image:27410
local file: /storage/emulated/0/Android/data/com.pc.wecare/files/images2116155570jpg

This is the exception that I got when i used exception.getMessage();

Excecption message: Object does not exist at location.

This are the details from LogCat. It shows from picking up a photo from gallery and uploading it to Firebase Storage:

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

解决方案

  1. please check your permission in manifest

  2. File.createTempFile( fileName, null, context.getCacheDir());

This is not file extension

please check your file path

/storage/emulated/0/Android/data/com.pc.wecare/files/images2116155570jpg images2116155570.jpg

这篇关于从firebase存储下载图片失败:android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆