Android长按imageview把图片保存到本地的实例代码
更新时间:2017年12月15日 16:26:22 作者:NotesChapter
本文通过代码给大家介绍了Android长按imageview把图片保存到本地的实现方法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
工具类
之前用
AsyncTask
现在改用rxJava
public class SaveImageUtils { public static void imageSave(final ImageView imageView, final int id) { Observable .create(new Observable.OnSubscribe<ImageView>() { @Override public void call(Subscriber<? super ImageView> sub) { sub.onNext(imageView); } } ).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new MySubscriber<ImageView>() { @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 MySubscriber<T> extends Subscriber<T> { @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<Bitmap, Void, String> { 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
<string name="save_picture">保存图片</string> <string name="save_picture_failed">保存失败</string> <string name="save_picture_success">图片成功保存至%s目录</string>
代码中引用
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 Flutter基于WebSocket实现即时通讯功能
WebSocket是一种在单个TCP连接上进行全双工通信的协议。本文将利用Flutter WebSocket实现即时通讯功能,文中示例代码讲解详细,感兴趣的可以了解一下2022-03-03Android高级组件ImageSwitcher图像切换器使用方法详解
这篇文章主要为大家详细介绍了Android高级组件ImageSwitcher图像切换器的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-12-12Android开发实现横向列表GridView横向滚动的方法【附源码下载】
这篇文章主要介绍了Android开发实现横向列表GridView横向滚动的方法,结合实例形式分析了Android横向列表GridView实现横向滚动的相关布局与功能实现技巧,并附带源码供读者下载参考,需要的朋友可以参考下2018-01-01
最新评论