Author: Panda Fang
Source: http://www.cnblogs.com/lonkiss/p/7119062.html
A way downloadOnly
Create a ImageActivity
public class ImageActivity extends AppCompatActivity {
private static final String TAG = "ImageActivity";
private ImageView iv;
Bitmap bitmap;
private String mUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
iv = (ImageView) findViewById(R.id.iv);
// 图片的url 从另一个Activity传过来
String url = getIntent().getStringExtra(EXTRA_URL);
mUrl = url;
loadImage(url);
// 长按 ImageView 把图片保存到手机
iv.setOnLongClickListener(new View.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
download(mUrl)
return true;
}
});
}
// 加载并显示图片
public void loadImage(String url) {
Glide.with(this).load(url).placeholder(R.drawable.image_loading)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.d(TAG, "onResourceReady: mode: " + model);
// 如果return true; 则 into(iv) 不起作用, 要手动设置图片
// iv.setImageDrawable(resource);
return false;
}
})
.into(iv);
}
// 保存图片到手机
public void download(final String url) {
new AsyncTask<Void, Integer, File>() {
@Override
protected File doInBackground(Void... params) {
File file = null;
try {
FutureTarget<File> future = Glide
.with(ImageActivity.this)
.load(url)
.downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
file = future.get();
// 首先保存图片
File pictureFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
File appDir = new File(pictureFolder ,"Beauty");
if (!appDir.exists()) {
appDir.mkdirs();
}
String fileName = System.currentTimeMillis() + ".jpg";
File destFile = new File(appDir, fileName);
FileUtil.copy(file, destFile);
// 最后通知图库更新
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(destFile.getPath()))));
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return file;
}
@Override
protected void onPostExecute(File file) {
Toast.makeText(ImageActivity.this, "saved in Pictures/GankBeauty", Toast.LENGTH_SHORT).show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
}.execute();
}
}
The above code from another activity get the picture url, then use Glide image loading library picture displayed on the ImageView, press ImageView can save the picture to your phone's Pictures / Beauty directory
This is the basic code templates, the reality is more complex, such as Android 6 issues also apply for permission to store runtime.
Second way asBitmap bitmap.compress
There is also a way to save the image there are a lot of people use, but after I compared the findings were not good, way above recommended. Another aspect of the code below, into the upper loadImage below loadBitmap
public void loadBitmap(String url) {
Glide.with(this).load(url).asBitmap().placeholder(R.drawable.image_loading)
.listener(new RequestListener<String, Bitmap>() {
@Override
public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.i(TAG, "onResourceReady: thread is " + Thread.currentThread().getName());
isReady = true;
bitmap = resource;
iv.setImageBitmap(resource);
return false;
}
})
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
}
The above download into the following saveImage
public void saveImage() {
// 首先保存图片
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
File appDir = new File(file ,"Beauty");
boolean createed = false;
if (!appDir.exists()) {
createed = appDir.mkdirs();
}
String fileName = System.currentTimeMillis() + ".jpg";
File currentFile = new File(appDir, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(currentFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 最后通知图库更新
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(currentFile.getPath()))));
}
In this way the picture becomes a bitmap, save time again compressed bitmap saved to the phone.
to sum up
Download pictures from your computer, and then saved to the phone's picture contrast, volume bitmap images found ways to get larger.
The first way to download a picture consistent with downloaded from the computer graphics volume, consistent md5.
It is recommended downloadOnly way