【发布时间】: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链接并且你想正确下载它吗?
-
是的,我有一个链接,而且我也知道有关令牌的一切,因为我将图像上传到存储,用户只需下载它们。
-
下载并保存在他的本地存储中,或者您只是想在您的应用中显示链接中的图像?
-
下载文件到本地内存图片预览已经显示,我只需要将图片下载到本地内存