gpt4 book ai didi

java - 将 GIF 文件保存到存储器

转载 作者:行者123 更新时间:2023-12-01 16:30:01 25 4
gpt4 key购买 nike

我想在内部或外部存储中保存 GIF 文件(在 Android 应用程序中)。然后应该可以在图库中找到该文件。我通过glide下载了gif文件:

    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();

最佳答案

您的使用ftech

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

Androidx 使用:

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

下载任意文件

    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();
}

}

已授予存储权限:

 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;
}
}

list :

   <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"/>

关于java - 将 GIF 文件保存到存储器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62067257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com