Reputation: 43
Hi then since new in android programming I would need a great help to solve this problem, then I am creating an image app, the images are uploaded by me on Firebase Storage and therefore I can access the link and tokens whenever I want, so when the user chooses an image he downloads the one he has chosen, now I have divided my app so I created as many fragments as there are images and the download button is contained in the fragment relating to the image now I show you how
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
}
});
}
}
now I show you the layout of the fragment
<?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>
now I show you 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();
}
}
now I would like to ask you a function to download the file already knowing the link and then manually inserting it for each image under the onClick function of each reference Button, I hope you can help me with many useful tips or necessary changes, thanks to all.
Upvotes: 0
Views: 929
Reputation: 43
guys I solved it like this:
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);
}
}
everything works perfectly download the required file, only that it saves everything in downloads, while I would like to download the photos in a precise folder inside the photos of the device so as to be easily found, so how do I save the downloaded file inside a path specified by me and precisely within the image gallery? thank you all
Upvotes: 1