【问题标题】:Download an image from Firebase Storage knowing the link知道链接从 Firebase 存储下载图像
【发布时间】:2020-05-28 18:02:43
【问题描述】:

嗨,既然是 android 编程的新手,我需要很大的帮助来解决这个问题,然后我正在创建一个图像应用程序,图像由我上传到 Firebase 存储,因此我可以随时访问链接和令牌,所以当用户选择一张图片时,他会下载他选择的图片,现在我已经划分了我的应用程序,所以我创建了与图片一样多的片段,并且下载按钮包含在与图片相关的片段中,现在我向您展示如何

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

    public class fragment_1 extends Fragment {
    Button bntDwn_1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        bntDwn_1 = (Button) getActivity().findViewById(R.id.dwn_1);
        bntDwn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //here I would like to insert the function to download the image directly

            }
        });
    }
}

现在我向你展示片段的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment_1">

    <ImageView
        android:id="@+id/sfondo_1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="@string/sfondo_1"
        android:src="@drawable/forpaper_1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/dwn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:text="@string/scarica"
        app:layout_constraintBottom_toBottomOf="@+id/sfondo_1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

现在我向你展示 MainActivity

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private StorageReference mStorageRef;
    public Random mRandom;
    public TextView mCounterCrd;

    int StringCrd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Inizializzazione Archiviazione Cloud
        mStorageRef = FirebaseStorage.getInstance().getReference();


        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tx =  fm.beginTransaction();
        fragment_1 fragmentview = new fragment_1();
        tx.replace(R.id.frame_place, fragmentview);
        tx.commit();
       }
    }

现在我想问你一个功能,下载已经知道链接的文件,然后在每个参考按钮的onClick功能下手动插入每个图像,希望你能帮助我提供许多有用的提示或必要的更改,谢谢大家。

【问题讨论】:

  • 你已经有firebaseimage链接并且你想正确下载它吗?
  • 是的,我有一个链接,而且我也知道有关令牌的一切,因为我将图像上传到存储,用户只需下载它们。
  • 下载并保存在他的本地存储中,或者您只是想在您的应用中显示链接中的图像?
  • 下载文件到本地内存图片预览已经显示,我只需要将图片下载到本地内存

标签: android firebase storage


【解决方案1】:

伙计们,我是这样解决的:

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

public class fragment_1 extends Fragment {

    private static final String DIRECTORY_DOWNLOADS = "CAMERA";
    Button bntDwn_1;
    FirebaseStorage firebaseStorage;
    StorageReference storageReference;
    StorageReference ref;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        bntDwn_1 = (Button) getActivity().findViewById(R.id.dwn_1);
        bntDwn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("fragment_1","Click su scarica fragment 1");
                download();
            }
        });
    }

    public void download(){
        storageReference= firebaseStorage.getInstance().getReference();
        ref = storageReference.child("fourapper_forpaper_1 (1).jpeg");
        ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String url = uri.toString();
                downloadFiles(getActivity(), "Sfondo", ".jpeg",DIRECTORY_DOWNLOADS,url);

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
            }
        });
    }

    private void downloadFiles(Context context, String fileName, String fileExtension, String destinationDirectory, String Url){
        DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(Url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName +fileExtension);
        downloadManager.enqueue(request);
    }
}

一切正常下载所需的文件,只是它将所有内容保存在下载中,而我想将照片下载到设备照片内的精确文件夹中以便于查找,那么如何保存下载的文件在我指定的路径内,并且恰好在图片库中?谢谢大家

【讨论】:

    猜你喜欢
    • 2016-12-28
    • 2020-10-15
    • 2017-01-16
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    相关资源
    最近更新 更多