这篇文章将为大家详细讲解有关Android应用中怎么将图片保存到本地相册,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
首先是保存图片到本地
private static final String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() :
"/mnt/sdcard";//保存到SD卡
private static final String SAVE_REAL_PATH = SAVE_PIC_PATH + "/good/savePic";
//保存的确切位置,根据自己的具体需要来修改
public void saveFile(Bitmap bm, String fileName, String path) throws IOException {
String subForder = SAVE_REAL_PATH + path;
File foder = new File(subForder);
if (!foder.exists()) {
foder.mkdirs();
}
File myCaptureFile = new File(subForder, fileName);
if (!myCaptureFile.exists()) {
myCaptureFile.createNewFile();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
以上就是保存图片的方法,保存完毕之后就是要通知相册刷新了,
在4.4中:
MediaScannerConnection.scanFile(this, new String[]{SAVE_REAL_PATH+ "/" + fileName}, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.e( "onScanCompleted: ", path);
Log.e( "onScanCompleted: ", uri.toString());
}
});
在4.4以上的是发送广播来实现:
Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED); //这是刷新SD卡
// Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); // 这是刷新单个文件
Uri uri = Uri.fromFile(new File(SAVE_REAL_PATH));
intent.setData(uri);
sendBroadcast(intent);
关于Android应用中怎么将图片保存到本地相册就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。