Android 如何使用滑动按钮将图像保存到存储器?
我正在使用glide进入recyclerview显示服务器上的图像。当用户单击图像时,它会显示完整的图像。我希望用户可以保存按钮点击图像Android 如何使用滑动按钮将图像保存到存储器?,android,android-glide,Android,Android Glide,我正在使用glide进入recyclerview显示服务器上的图像。当用户单击图像时,它会显示完整的图像。我希望用户可以保存按钮点击图像 imageViewPreview = (ImageView) view.findViewById(R.id.image_preview); Photos image = images.get(position); Glide.with(getActivity()) .load(image.ge
imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
Photos image = images.get(position);
Glide.with(getActivity())
.load(image.getUrl())
.thumbnail(Glide.with(getActivity()).load(R.drawable.loader))
.fitCenter()
.crossFade()
.into(imageViewPreview);
在调用intoimageViewPreview之前,可以尝试以位图的形式加载
Glide.with(getActivity())
.load(image.getUrl())
.asBitmap()
... rest of your stuffs
然后访问位图
Bitmap bitmap = ((BitmapDrawable)imageViewPreview.getDrawable()).getBitmap();
在调用intoimageViewPreview之前,可以尝试以位图的形式加载
Glide.with(getActivity())
.load(image.getUrl())
.asBitmap()
... rest of your stuffs
然后访问位图
Bitmap bitmap = ((BitmapDrawable)imageViewPreview.getDrawable()).getBitmap();
因为有很多方法可以实现这一点,如果有一个最简单的解决方案是使用UniversalIMageLoader,这里是实现指南 下面是如何下载图像的实现
ImageLoader.getInstance().loadImage(
"IMAGE URL",
new ImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
//SHOW PROGRESS
}
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
//HIDE PROGRESS
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//HIDE PROGRESS
//Use this Bitmap to save in to galler
FileOutputStream out = null;
try {
out = new FileOutputStream("FILE_NAME");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onLoadingCancelled(String s, View view) {
//HIDE PROGRESS
}
}
);
因为有很多方法可以实现这一点,如果有一个最简单的解决方案是使用UniversalIMageLoader,这里是实现指南 下面是如何下载图像的实现
ImageLoader.getInstance().loadImage(
"IMAGE URL",
new ImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
//SHOW PROGRESS
}
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
//HIDE PROGRESS
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//HIDE PROGRESS
//Use this Bitmap to save in to galler
FileOutputStream out = null;
try {
out = new FileOutputStream("FILE_NAME");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onLoadingCancelled(String s, View view) {
//HIDE PROGRESS
}
}
);
解决办法
解决办法
如果要使用Glide加载图像并保存它,则可以以下方式使用Glide:
Glide.with(getApplicationContext())
.load("https://i.stack.imgur.com/quwoe.jpg")
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
saveImage(resource);
}
});
//code to save the image
private void saveImage(Bitmap resource) {
String savedImagePath = null;
String imageFileName = "image" + ".jpg";
final File storageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Pics");
boolean success = true;
if(!storageDir.exists()){
success = storageDir.mkdirs();
}
if(success){
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
resource.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
}
}
// Add the image to the system gallery
private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
如果要使用Glide加载图像并保存它,则可以以下方式使用Glide:
Glide.with(getApplicationContext())
.load("https://i.stack.imgur.com/quwoe.jpg")
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
saveImage(resource);
}
});
//code to save the image
private void saveImage(Bitmap resource) {
String savedImagePath = null;
String imageFileName = "image" + ".jpg";
final File storageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Pics");
boolean success = true;
if(!storageDir.exists()){
success = storageDir.mkdirs();
}
if(success){
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
resource.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
}
}
// Add the image to the system gallery
private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
因此,您基本上希望从imageview@VladyslavMatviienko是。@quicklearner我想将图像加载到imageview中。如果用户单击“保存”按钮,则应保存图像。加载图像时,您可以直接保存图像,并在用户单击按钮时显示toast/alert;如果用户未单击按钮,则您可以删除文件,如果您只想使用GLIDE,那么您基本上希望从imageview@VladyslavMatviienko是。@quicklearner我要加载图像进入imageview。如果用户单击“保存”按钮,则应保存图像。您可以在加载图像时直接保存图像,并在用户单击按钮时显示toast/警报,如果用户未单击按钮,则如果您只想在toast、、中使用GLIDE,则可以删除该文件,,,导入另一个//取消注释此行ImageView Imgv=ImageView mPager.findViewWithTagmPager.getCurrentItem;什么是myDir?什么是mPager?为什么有些变量是根据Java代码约定命名的?什么是Imgv?此外,它将不保存原始图像,而仅保存图形缓存。因此,如果您的ImageView大小为200x200px,而原始图像为2000x2000,它将保存200x200,我不是问问题的人。我不打算用它。更新后再用一次。如果适合你的需要,把答案标记为correct@MukeshSonist in Toast,,,导入另一个//取消注释此行ImageView Imgv=ImageView mPager.findViewWithTagmPager.getCurrentItem;什么是myDir?什么是mPager?为什么有些变量是根据Java代码约定命名的?什么是Imgv?此外,它将不保存原始图像,而仅保存图形缓存。因此,如果您的ImageView大小为200x200px,而原始图像为2000x2000,它将保存200x200,我不是问问题的人。我不打算用它。更新后再用一次。如果适合你的需要,把答案标记为correct@MukeshSoni您可以通过使用->在图像视图中使用Glide来显示图像,但根据我的说法,使用Glide的输入功能进行保存和显示不能同时进行。如果我错了,请纠正我。您可以使用->在图像视图中使用Glide显示图像,但根据我的说法,使用Glide的输入功能进行保存和显示不能同时进行。如果我错了,请纠正我。Universal Image Loader库使用Android的BitmapFactory.decodeStream,这在网络不可靠的情况下是有缺陷的,用户可能会看到半个空图像。Glide不使用这一点,因此总是一个更好的选择。Universal Image Loader库使用Android的BitmapFactory.decodeStream,在网络不可靠的情况下,该库存在缺陷,用户可能会看到半个空图像。Glide不使用该选项,因此总是一个更好的选择。