Android中怎么利用imageview將圖片保存到本地,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
大洼ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
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; } });
關(guān)于Android中怎么利用imageview將圖片保存到本地問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
文章題目:Android中怎么利用imageview將圖片保存到本地
文章轉(zhuǎn)載:http://jinyejixie.com/article16/ghddgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、電子商務(wù)、域名注冊、軟件開發(fā)、ChatGPT、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)