欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android长按imageview把图片保存到本地的实例代码

时间:2021-06-20 08:51:12|栏目:Android代码|点击:

工具类

之前用

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 七种进度条的样式

栏    目:Android代码

下一篇:Android 解决TextView排版参差不齐的问题

本文标题:Android长按imageview把图片保存到本地的实例代码

本文地址:http://www.codeinn.net/misctech/145351.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有