Bitmap bmp = BitmapFactory.decodeFile("/sdcard/FaceDetect/facedone.jpg");
String filefolder =Environment.getExternalStorageDirectory()+"/FaDa/";
File parent = new File(filefolder);
if (!parent.exists()) {
//若不存在,创建目录,可以在应用启动的时候创建
parent.mkdirs();
Log.i("llll","parent = " + parent.exists());
}
String path = filefolder + picName + ".jpg";
File file = new File(path);
String paht1=file.getAbsolutePath();
Log.i("llll","path1 = " + path);
if(file.exists()){
file.delete();
}
try {
file.createNewFile();
Log.i("llll","createNewFile");
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fOut = null;
try {
Log.i("llll","createNewFile");
fOut = new FileOutputStream(file);
}catch (FileNotFoundException e){
e.printStackTrace();
}
Log.i("llll","fOut = " +(fOut != null));
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try {
fOut.flush();
fOut.close();
}catch (IOException e){
e.printStackTrace();
}
}
3 个解决方案
#1
你保存的方法不对。这里有一句if(file.exists()){
file.delete();
},也就是说如果该目录下有这个文件,就要进行删除。所以才会出现你说的那种情况。
file.delete();
},也就是说如果该目录下有这个文件,就要进行删除。所以才会出现你说的那种情况。
#2
//图片压缩
Bitmap bitmap = compressBmpFromBmp(compressImageFromFile(filePath));
// Bitmap bitmap=compressBmpFromBmp(filePath);
//先判读是否有路径
makeRootDirectory(Utils.getAppPathPhoto_beging());
String saveImgPath = Utils.getAppPathPhoto_beging() + name + ".jpg";
File saveimg = new File(saveImgPath);
FileOutputStream fos;
File uploadImg;
try {
fos = new FileOutputStream(saveimg);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
public static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
}
}
#3
我的代码是对的,我手机出了点bug,谢谢大家
#1
你保存的方法不对。这里有一句if(file.exists()){
file.delete();
},也就是说如果该目录下有这个文件,就要进行删除。所以才会出现你说的那种情况。
file.delete();
},也就是说如果该目录下有这个文件,就要进行删除。所以才会出现你说的那种情况。
#2
//图片压缩
Bitmap bitmap = compressBmpFromBmp(compressImageFromFile(filePath));
// Bitmap bitmap=compressBmpFromBmp(filePath);
//先判读是否有路径
makeRootDirectory(Utils.getAppPathPhoto_beging());
String saveImgPath = Utils.getAppPathPhoto_beging() + name + ".jpg";
File saveimg = new File(saveImgPath);
FileOutputStream fos;
File uploadImg;
try {
fos = new FileOutputStream(saveimg);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
public static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
}
}
#3
我的代码是对的,我手机出了点bug,谢谢大家