android图片的内部存储,android将图像保存到内部存储

要在sdcard中保存位图,请使用以下代码

存储图像

private void storeImage(Bitmap image) {

File pictureFile = getOutputMediaFile();

if (pictureFile == null) {

Log.d(TAG,

"Error creating media file, check storage permissions: ");// e.getMessage());

return;

}

try {

FileOutputStream fos = new FileOutputStream(pictureFile);

image.compress(Bitmap.CompressFormat.PNG, 90, fos);

fos.close();

} catch (FileNotFoundException e) {

Log.d(TAG, "File not found: " + e.getMessage());

} catch (IOException e) {

Log.d(TAG, "Error accessing file: " + e.getMessage());

}

}

获取图像存储的路径

/** Create a File for saving an image or video */

private File getOutputMediaFile(){

// To be safe, you should check that the SDCard is mounted

// using Environment.getExternalStorageState() before doing this.

File mediaStorageDir = new File(Environment.getExternalStorageDirectory()

+ "/Android/data/"

+ getApplicationContext().getPackageName()

+ "/Files");

// This location works best if you want the created images to be shared

// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist

if (! mediaStorageDir.exists()){

if (! mediaStorageDir.mkdirs()){

return null;

}

}

// Create a media file name

String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());

File mediaFile;

String mImageName="MI_"+ timeStamp +".jpg";

mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);

return mediaFile;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值