| | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.BitmapFactory; |
| | | import android.graphics.Canvas; |
| | | import android.media.MediaScannerConnection; |
| | | import android.net.Uri; |
| | |
| | | import com.hdl.hdllinphonesdk.R; |
| | | |
| | | import java.io.BufferedOutputStream; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.lang.ref.SoftReference; |
| | | |
| | | /** |
| | | * Created by jlchen on 2021/8/16. |
| | |
| | | |
| | | } |
| | | } catch (FileNotFoundException e) { |
| | | Log.e("FileNotFoundException", "FileNotFoundException:" + e.getMessage().toString()); |
| | | HDLLog.e("FileNotFoundException", "FileNotFoundException:" + e.getMessage().toString()); |
| | | e.printStackTrace(); |
| | | return false; |
| | | } catch (IOException e) { |
| | | Log.e("IOException", "IOException:" + e.getMessage().toString()); |
| | | HDLLog.e("IOException", "IOException:" + e.getMessage().toString()); |
| | | e.printStackTrace(); |
| | | return false; |
| | | } catch (Exception e) { |
| | | Log.e("IOException", "IOException:" + e.getMessage().toString()); |
| | | HDLLog.e("IOException", "IOException:" + e.getMessage().toString()); |
| | | e.printStackTrace(); |
| | | return false; |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 保存图片至相册 |
| | | * 需要读写权限 |
| | | */ |
| | | public static boolean saveImageToGallery(Context context, byte[] image_bytes) { |
| | | Bitmap bitmap = byteToBitmap(image_bytes); |
| | | return saveImageToGallery(context, bitmap); |
| | | } |
| | | |
| | | public static Bitmap byteToBitmap(byte[] imgByte) { |
| | | InputStream input = null; |
| | | Bitmap bitmap = null; |
| | | BitmapFactory.Options options = new BitmapFactory.Options(); |
| | | options.inSampleSize = 1; |
| | | input = new ByteArrayInputStream(imgByte); |
| | | SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options)); //软引用防止OOM |
| | | bitmap = (Bitmap) softRef.get(); |
| | | if (imgByte != null) { |
| | | imgByte = null; |
| | | } |
| | | |
| | | try { |
| | | if (input != null) { |
| | | input.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | // 异常捕获 |
| | | e.printStackTrace(); |
| | | } |
| | | return bitmap; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将文件保存到公共的媒体文件夹 |