// 给定一个Bitmap,进行保存
public void saveJpeg(Bitmap bm) {
String savePath = null;
if (intcount <= 1500) {
savePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "DCIM/Camera";
Log.e(TAG, "save bitmap的存储路径是 :" + savePath + "," + "incount的值是:" + intcount);
File folder = new File(savePath);
if (!folder.exists()) // 如果文件夹不存在则创建
{
folder.mkdir();
}
} else if (intcount >= 1501) {
savePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "DCIM/Camera"
+ String.valueOf(1000);
Log.e(TAG, "save bitmap的存储路径是 :" + savePath + "," + "incount的值是:" + intcount);
File folder2 = new File(savePath);
if (!folder2.exists()) // 如果文件夹不存在则创建
{
folder2.mkdir();
}
}
long dataTake = System.currentTimeMillis();
String jpegName = savePath + File.separator +"IMG_"+ dataTake +"_"+intcount+"_"+ ".jpg";
Log.e(TAG, "saveJpeg:jpegName--" + jpegName);
File jpegFile = new File(jpegName);
try {
FileOutputStream fout = new FileOutputStream(jpegName);
BufferedOutputStream bos = new BufferedOutputStream(fout);
// TODO: 2018/8/8 100就表示 不压缩
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
// TODO: 2018/8/9 保存到图片库 ,可以看到
MediaStore.Images.Media.insertImage(CameraAct.this.getContentResolver(),
jpegFile.getAbsolutePath(), jpegName, null);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(new File(jpegName));
System.out.println(jpegName+"uriuri:::"+uri);
intent.setData(uri);
sendBroadcast(intent);
bos.flush();
bos.close();
mCamera.stopPreview();
mCamera.startPreview();
bm.recycle();
// succCount = succCount + 1;
Log.e(TAG, "saveJpeg:存储完毕!-->" + succCount);
} catch (IOException e) {
Log.e(TAG, "saveJpeg:存储失败!-->" + failCount);
e.printStackTrace();
}
}
Android 调用系统的API拍照,在图片库中显示
最新推荐文章于 2024-03-01 19:19:57 发布
本文介绍了一种将Bitmap对象保存为JPEG格式图片的方法,并详细解释了如何根据计数选择存储路径,以及如何利用Android系统API完成图片的压缩、保存及更新系统相册。
1740

被折叠的 条评论
为什么被折叠?



