我在我的 android 应用程序中使用裁剪功能,它工作正常,但我想将裁剪的图像保存到我的SD Card
. 为此需要遵循哪些步骤?
user1855796
问问题
5402 次
3 回答
1
试试这个 :
public void saveBitmap(Bitmap bmp)
{
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/NewFolder";
File dir = new File(file_path);
if(!dir.exists)
dir.mkdirs();
File file = new File(dir, "myImage.png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
文件中需要以下权限Manifest
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
谢谢。
于 2013-01-03T09:15:21.867 回答
1
首先在 sdcard 中获取图像存储:-
public static String storeImage(Bitmap bitmap, String filename) {
String stored = null;
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, filename + ".png");
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
stored = "success";
} catch (Exception e) {
e.printStackTrace();
}
return stored;
}
第二个从 sdcard 获取图像:-
public static File getImage(String imagename) {
File mediaImage = null;
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
if (!myDir.exists())
return null;
mediaImage = new File(myDir.getPath() + imagename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mediaImage;
}
3 将图像从 sdcard 转换为位图
File file = CommonUtils.getImage("/CoverPic.png");
String path = file.getAbsolutePath();
if (path != null)
picture = BitmapFactory.decodeFile(path);
于 2014-01-29T07:42:11.517 回答
0
如果Bitmap
你裁剪的被称为yourBitmap
File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, "filename.png");
FileOutputStream out = new FileOutputStream(f);
yourBitmap.compress(Bitmap.CompressFormat.PNG, 90, out)
于 2013-01-03T09:14:01.857 回答