标签:show str static and long storage state 也有 权限
两种方式, 推荐方式一
创建一个 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(); } }
以上代码 从另一个activity得到 图片的 url , 然后使用Glide 图片加载库 将图片显示到 ImageView上, 长按ImageView可以将图片保存到手机的 Pictures/Beauty 目录中
这是基本的代码模板, 实际情况更复杂,比如Android 6 还有运行时申请存储权限的问题。
还有一种保存图片的方式也有很多人用,但是经过我对比发现并不好,推荐上面的方式。另一种方式的代码如下
把上面 loadImage 换成 下面的 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); }
把上面的 download 换成下面的 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())))); }
这种方式把图片变成bitmap , 保存的时候又重新压缩bitmap 保存到手机。
从电脑下载图片,然后与保存到手机的图片对比,发现bitmap的方式得到的图片体积变大。
而第一种方式下载的图片与从电脑上下载的图片体积一致,md5一致。
所以推荐downloadOnly方式
作者:Panda Fang
出处:http://www.cnblogs.com/lonkiss/p/7119062.html
原创文章,转载请注明作者和出处,未经允许不可用于商业营利活动
标签:show str static and long storage state 也有 权限
原文地址:http://www.cnblogs.com/lonkiss/p/7119062.html