[英]Best way to download image from URL and save it into internal storage memory
I am developing an app in which I want to download image from URL.我正在开发一个应用程序,我想在其中从 URL 下载图像。 I need to download these images at once and stored it into internal storage.
我需要立即下载这些图像并将其存储到内部存储器中。 There are more than 200 images for downloading.
有200多张图片可供下载。 Please tell me the best way to download these images at minimum time possible.
请告诉我在尽可能短的时间内下载这些图像的最佳方式。 If any third part library is available the please tell.
如果有任何第三方库可用,请告诉。
Consider using Picasso for your purpose.考虑将毕加索用于您的目的。 I'm using it in one of my project.
我在我的一个项目中使用它。 To save image on external disk you can use following:
要将图像保存在外部磁盘上,您可以使用以下方法:
Picasso.with(mContext)
.load(ImageUrl)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/yourDirectory");
if (!myDir.exists()) {
myDir.mkdirs();
}
String name = new Date().toString() + ".jpg";
myDir = new File(myDir, name);
FileOutputStream out = new FileOutputStream(myDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch(Exception e){
// some action
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
);
You can download th image from an url like this:您可以从这样的网址下载图像:
URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
And you may then want to save the image so do:然后您可能想要保存图像,这样做:
FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg");
fos.write(response);
fos.close();
Ok first of all implement that library into gradle of app level for load the image from url into image view: 好吧,首先将该库实现到应用程序级别的gradle中,以将图像从URL加载到图像视图中:
implementation("com.github.bumptech.glide:glide:4.6.1")
{
exclude group: "com.android.support"
}
Now write below code in your activity for load the image:: 现在在您的活动中编写以下代码以加载图像::
Glide.with(this).load("url of your image").thumbnail(Glide.with(this).load(R.drawable.loadingsingleimage)).into(imageView);
Now download it into the your device storage by implementing the following code.. 现在,通过实现以下代码将其下载到设备存储中。
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/MotivationalQuotes/");
dir.mkdir();
File file = new File(dir, System.currentTimeMillis() + ".png");
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
try {
outputStream.flush();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
try {
outputStream.close();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "Downloaded into MotivationalQuotes.", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Image is not loaded yet...", Toast.LENGTH_SHORT).show();
}
}
});
BitmapDrawable bitmapDrawable = (BitmapDrawable) holder.post_image.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bitmap, "IMG_" + Calendar.getInstance().getTime(), null);
It is quite a simple job...这是一个很简单的工作......
const actionDownloadImage = (urls) => {
urls.map((url) => {
const splitUrl = url.split("/");
const filename = splitUrl[splitUrl.length - 1];
fetch(url)
.then((response) => {
response.arrayBuffer().then(function (buffer) {
const url = window.URL.createObjectURL(new Blob([buffer]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", filename); //or any other extension
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
})
.catch((err) => {
console.log(err);
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.