简体   繁体   中英

Save GIF file to storage

I want to save a GIF file (in android app) in internal or external storage. The file should then be found in the gallery. I downloaded the gif file via glide:

    Glide.with(getContext())
    .download(imageUrl)
    .listener(new RequestListener<File>() {
    @Override
    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
return false;
}
 @RequiresApi(api = Build.VERSION_CODES.O)
@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
}
return false;


    }
                                                        })
                                                        .submit();

your use ftech

implementation "com.tonyodev.fetch2:fetch2:3.0.10"

Androidx use:

implementation "androidx.tonyodev.fetch2:xfetch2:3.1.4"

download any file

    if (isStoragePermissionGranted()) {
                    fetch = Fetch.Impl.getInstance(fetchConfiguration);

                    String url = nameFileDownload;

                    String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + nameFileDownload.substring(url.lastIndexOf('/') + 1);
                    ;

                    if (fileExist(file)) {
                        view(null, nameFileDownload.substring(url.lastIndexOf('/') + 1));

                    } else {
                        progress_download.setVisibility(View.VISIBLE);
                        btn_download.setVisibility(View.GONE);
                        final com.tonyodev.fetch2.Request request = new com.tonyodev.fetch2.Request(url, file);
                        request.setPriority(Priority.NORMAL);
                        request.setNetworkType(NetworkType.ALL);

                        fetch.enqueue(request, updatedRequest -> {


                            fetch.addListener(fetchListener);


                            //Request was successfully enqueued for download.
                        }, error -> {
                            progress_download.setVisibility(View.GONE);
                            btn_download.setVisibility(View.VISIBLE);
                            Toast.makeText(MainActivity.this, "feild", Toast.LENGTH_SHORT).show();

                            //An error occurred enqueuing the request.
                        });
                    }

                } else {
                    Toast.makeText(MainActivity.this, "perssion denid", Toast.LENGTH_SHORT).show();
                }

            }

isStoragePermissionGranted:

 public boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("TAG", "Permission is granted");
            return true;
        } else {

            Log.v("TAG", "Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    } else { //permission is automatically granted on sdk<23 upon installation
        Log.v("TAG", "Permission is granted");
        return true;
    }
}

manifests:

   <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM