在我的Android项目中,我尝试将位图保存到指定位置的SD卡上。压缩位图时的Android IOException
当我运行这个,我得到一个IOException
每一次,说权限被拒绝。这是创建FileOutputStream。
这使我相信我失去了权限,但我包括READ and WRITE_EXTERNAL_STORAGE
。它们在我的uses-sdk
块之前声明,并位于应用程序块之外。我也read/write
文本到我的应用程序中的其他地方的文件,并且工作得很好。我目前的代码已被修改为与我在网上找到的几种解决方案相匹配,但我无法成功保存bitmap
文件。
如果有人能指出我的错误,我会非常感激。
ImageView imageView = (ImageView) findViewById(R.id.new_pnm_pic);
Bitmap bm = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bm);
String outPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Recruits";
try {
File d = new File(outPath);
if (!d.exists())
d.mkdirs();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
String currentTime = sd.format(new Date());
File file = new File(outPath+'/'+currentTime+".jpg");
FileOutputStream fOut = new FileOutputStream(file.getPath());
bm.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
fOut.flush();
fOut.close();
}
catch(IOException ioe){
Toast.makeText(this, "Nice try but didn't work", Toast.LENGTH_SHORT).show();
}
如果使用Environment.getExternalStorageDirectory()。toString()+“Recruits”定义outPath,它会起作用吗?我有一个getAbsolutePath()类似的问题,它解决了它。做一个日志。在logcat中查看完整路径。 – Opiatefuchs
是否在模拟器上运行这个 – Sam
我将outPath改为 Environment.getExternalStorageDirectory()。toString()+“/ Recruits”; 和使用日志,看看我的文件是使用以下路径 W¯¯测试// MNT/SD卡/新兵/ 2013-06-01-17-26-50.jpg(23309) 当我试图测试后我创建了日志没有显示的FileOutputStream,这意味着这是异常。我也尝试使用替代构造函数FileOutputStream(File),我仍然得到这个异常。 山姆:我在我的设备上运行这个 – user2442678