使用url从firebase下载图像到外部SD卡

时间:2018-03-09 12:03:19

标签: android firebase url uri android-external-storage

我已将图像文件上传到我的firebase中。我已将url存储到firebase-database。在存储过程中,要获取我使用的网址Uri downloadUri = taskSnapshot.getDownloadUrl(); 现在我想在点击按钮时从该URL下载该图像文件,并希望将该图像文件存储到SD卡的特定文件夹中,然后说“#34;新文件夹" 帮我处理以下代码......

FirebaseStorage mFirebaseStorage;
StorageReference httpsReference;

mFirebaseStorage = FirebaseStorage.getInstance();
httpsReference = mFirebaseStorage.getReferenceFromUrl(downloadUri);

Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //what??
    }
});

3 个答案:

答案 0 :(得分:0)

您需要实施FileDownloadTask;见example

Environment.getExternalStorageDirectory()是SD卡,可能需要请求权限android.permission.READ_EXTERNAL_STORAGE才能访问该路径。例如。 https://developer.android.com/training/data-storage/files.html

答案 1 :(得分:0)

首先创建数据库引用和像这样的文件

$add1 = 50;
$sql = "SELECT sum FROM table_name WHERE id = '$id' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);
$add2 = $row['sum'] + $add1 ;
}
  $sql1 = "UPDATE tabel_name SET sum = '$add2' WHERE id = '$id' "; 
  $result1 = $conn->query($sql1);

比在此引用上调用getFile

StorageReference downloadRef = FirebaseStorage.getInstance().getReference().child("Wall/" + category
            + "/" + wall_id + ".jpg");
    File localFile = null;
    try {
        String fileName = wall_id + ".jpg";
        localFile = new File();//create your file with desired path
    } catch (Exception e) {
        e.printStackTrace();
    }

文件将保存到给定路径。

答案 2 :(得分:0)

考虑将毕加索用于你的目的。要将图像保存在外部磁盘上,您可以使用以下命令:

Picasso.with(mContext)
    .load(ImageUrl)
    .into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            try {
                String root = Environment.getExternalStorageDirectory().toString();
                File myDir = new File(root + "/yourDirectory");

                if (!myDir.exists()) {
                    myDir.mkdirs();
                }

                String name = new Date().toString() + ".jpg";
                myDir = new File(myDir, name);
                FileOutputStream out = new FileOutputStream(myDir);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

                out.flush();
                out.close();                        
            } catch(Exception e){
                // some action
            }
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
        }
    }
);

From here您可以下载此库。 别忘了在清单文件中添加权限