Android中怎么利用imageview将图片保存到本地
Android中怎么利用imageview将图片保存到本地,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
大洼ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
rxJava
public class SaveImageUtils { public static void imageSave(final ImageView imageView, final int id) { Observable .create(new Observable.OnSubscribe() { @Override public void call(Subscriber super ImageView> sub) { sub.onNext(imageView); } } ).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new MySubscriber () { @Override public void onNext(ImageView imageView) { File imageFile = new File(ActivityUtils.ImagePath(), id + ".jpg"); FileOutputStream outStream = null; try { outStream = new FileOutputStream(imageFile); Bitmap image = imageView.getDrawingCache(); image.compress(Bitmap.CompressFormat.JPEG, 100, outStream); outStream.flush(); outStream.close(); Toast.makeText(UIUtils.getContext(), UIUtils.getString(R.string.save_picture_success), Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); onError(e); } } @Override public void onError(Throwable e) { Toast.makeText(UIUtils.getContext(), UIUtils.getString(R.string.save_picture_failed), Toast.LENGTH_LONG).show(); } }); } }
MySubscriber
public class MySubscriberextends Subscriber { @Override public void onStart() { super.onStart(); } @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(T t) { } } //不建议使用了 package com.app.mly.utils; import android.app.Activity; import android.graphics.Bitmap; import android.os.AsyncTask; import android.os.Environment; import android.widget.ImageView; import android.widget.Toast; import com.fengche.android.mly.R; import java.io.File; import java.io.FileOutputStream; /** * Created by y on 2016/1/14. */ public class SaveImageUtils extends AsyncTask { Activity mActivity; ImageView mImageView; public SaveImageUtils(Activity activity, ImageView imageView) { this.mImageView = imageView; this.mActivity = activity; } @Override protected String doInBackground(Bitmap... params) { String result = mActivity.getResources().getString(R.string.save_picture_failed); try { String sdcard = Environment.getExternalStorageDirectory().toString(); File file = new File(sdcard + "/123"); if (!file.exists()) { file.mkdirs(); } File imageFile = new File(file.getAbsolutePath(), "命名" + ".jpg"); FileOutputStream outStream = null; outStream = new FileOutputStream(imageFile); Bitmap image = params[0]; image.compress(Bitmap.CompressFormat.JPEG, 100, outStream); outStream.flush(); outStream.close(); result = mActivity.getResources().getString(R.string.save_picture_success, file.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } return result; } @Override protected void onPostExecute(String result) { Toast.makeText(mActivity, result, Toast.LENGTH_SHORT).show(); mImageView.setDrawingCacheEnabled(false); } }
stirng文件下需要的几个toast
保存图片 保存失败 图片成功保存至%s目录
代码中引用
mScanCode.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(ShareActivity.this); builder.setItems(new String[]{getResources().getString(R.string.save_picture)}, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mScanCode.setDrawingCacheEnabled(true); Bitmap imageBitmap = mScanCode.getDrawingCache(); if (imageBitmap != null) { new SaveImageUtils(ShareActivity.this, mScanCode).execute(imageBitmap); } } }); builder.show(); return true; } });
关于Android中怎么利用imageview将图片保存到本地问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
文章标题:Android中怎么利用imageview将图片保存到本地
当前链接:http://scjbc.cn/article/ghddgg.html