我在我的应用程序中显示图像,我想在用户点击每个图像后添加下载按钮,图像将自动保存到文件夹。可能吗?直接从Android活动下载图像
0
A
回答
0
您可以使用Picasso库来显示图像。在您的build.gradle依赖,加入这个下面的代码:
compile 'com.squareup.picasso:picasso:2.4.0'
现在用这个来显示图像,您可以将按钮的onClick()方法中添加以下代码。
File file = new File(imagePath);
if(file.exists()) {
Picasso.with(context).load(file).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView);
}
else {
Picasso.with(context).load(imageUrl).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView, new PicassoCallBack(yourImageView,imagePath));
}
的picassoCallBack类将是这样的:
public class PicassoCallBack extends Callback.EmptyCallback {
ImageView imageView;
String filename;
public PicassoCallBack(ImageView imageView, String filename) {
this.imageView = imageView;
this.filename = filename;
}
@Override public void onSuccess() {
// Log.e("picasso", "success");
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
try {
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos1);
// FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
File file = new File(filename);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(baos1.toByteArray());
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError() {
Log.e("picasso", "error");
}
}
希望它会做你的工作。
0
如果你已经保存在您的应用程序文件,将它复制到这个公用文件夹 File imagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
然后使用该技术提供here扫描图片中的画廊。现在当用户打开图库时,他们会看到图片。
0
private static void persistImage(Bitmap bitmap, String name) {
File filesDir = getAppContext().getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
}
0
可以使用滑翔下载图像,我认为这是更好然后毕加索因为它扩展为picasso.and更多信息,请参阅https://github.com/bumptech/glide。 这个,你只需要包括compile 'com.github.bumptech.glide:glide:3.6.1'
到依赖,然后简单地添加此行代码
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);`
其中http://goo.gl/gEgYUd
是URL使用这个你没有保持高速缓存后pass.and。
享受您的代码:)
0
我试图Universal Image Loader而Picasso之前。 你看到你需要一个巫婆就够了。 也有更好的决定阅读this one和this one。 它可以帮助你:
相关问题
- 1. 直接从链接下载图片
- 2. 将图像直接下载到相册
- 3. 从quickblox下载图像android
- 4. 从C#中的直接身份验证链接下载图像
- 5. 直接从url下载图片
- 6. Android下载图像
- 7. Android图像下载
- 8. 下载图像并在android的活动中显示
- 9. 直接在Eclipse中启动Android活动
- 10. 从网站.vcs下载日历活动到Android下载
- 11. 从android通用图像加载器下载图像流
- 12. 从Android中的线程下载图像
- 13. 关于从Android下载图像
- 14. Android ViewPager:从URL下载图像
- 15. 从服务器下载Android图像?
- 16. 从URL序列下载Android图像?
- 17. 如何在android webview中直接下载链接下载文件
- 18. 在Android上监控WIFI直接活动
- 19. 如何运行活动指示器直到下载图像完成
- 20. 图像链接不下载图像
- 21. 自动从服务器下载图像
- 22. 从url下载图像到UIImageVIew动态
- 23. android下载/缓存图像
- 24. 下载图像 - Android电子
- 25. 在android中下载图像
- 26. 下载图像在Android
- 27. 直接链接到活动
- 28. 图像下载链接 - OpenERP
- 29. 如何运行活动指标直到下载图片?
- 30. 活动指示器直到加载下一个视图
均来自互联网或您的应用程序中的图像? – geokavel
这些图像来自哪里?它来自服务器还是您的应用程序? –