我在服务器上显示图像并滑入recyclerview。当用户点击图像时,它显示完整图像。我希望用户可以在点击按钮时保存该图像。
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);
答案 0 :(得分:2)
如果您想使用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);
}
答案 1 :(得分:0)
final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/SmartPhoto");
boolean success = false;
public void SaveImage() {
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "image" + n + ".png";
myDir.mkdirs();
File image = new File(myDir, fname);
imageview.setDrawingCacheEnabled(true);
Bitmap bitmap = Imgv.getDrawingCache();
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), R.string.savedimage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), R.string.errorloadingimage, Toast.LENGTH_LONG).show();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
final Uri contentUri = Uri.fromFile(image);
scanIntent.setData(contentUri);
sendBroadcast(scanIntent);
} else {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://mnt/sdcard/" + Environment.getExternalStorageDirectory())));
}
}
答案 2 :(得分:0)
您可以在调用into(imageViewPreview)
之前尝试加载为位图Glide.with(getActivity())
.load(image.getUrl())
.asBitmap()
... rest of your stuffs
然后访问位图
Bitmap bitmap = ((BitmapDrawable)imageViewPreview.getDrawable()).getBitmap();
答案 3 :(得分:0)
有很多方法可以实现这一点,如果有一个最简单的解决方案就是使用UniversalIMageLoader
这里是实施指南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
}
}
);
答案 4 :(得分:0)
解决方案
// DownloadImage AsyncTask
private class DownloadImageCertIV extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
File destination = new File(getActivity().getCacheDir(),
"your path" + ".jpg");
try {
destination.createNewFile();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(destination);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
要在按钮上使用此功能,请单击
new DownloadImageCertIV().execute("URL HERE");