1.保存图片文件到SD卡
private InputStream Bitmap2IS(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); InputStream sbs = new ByteArrayInputStream(baos.toByteArray()); return sbs; } public void onClick_SaveImageToSDCard(View view) { try { FileOutputStream fos = new FileOutputStream(ALBUM_PATH + "/QRcode.png"); // InputStream is = getResources().getAssets().open("image.jpg"); InputStream is = Bitmap2IS(bit); byte[] buf = new byte[4096]; int count = 0; while ((count = is.read(buf)) >= 0) { fos.write(buf, 0, count); } fos.close(); is.close(); /* * byte[] buffer = new byte[8192]; int count = 0; while((count = * is.read(buffer)) >= 0){ fos.write(buffer,0,count); } fos.close(); * is.close(); */ Toast.makeText(this, "写入成功!", Toast.LENGTH_LONG).show(); } catch (Exception e) { // TODO: handle exception Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } }
时间: 2024-11-08 19:14:42