我有一个问题,在我的Android应用程序上创建一个目录并将文件保存到它.我正在使用这段代码来执行此操作:
String filename = "MyApp/MediaTag/MediaTag-"+objectId+".png";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
fos = new FileOutputStream(file);
fos.write(mediaTagBuffer);
fos.flush();
fos.close();
Run Code Online (Sandbox Code Playgroud)
但这是一个例外:
java.io.FileNotFoundException:/mnt/sdcard/MyApp/MediaCard/MediaCard-0.png(没有这样的文件或目录)
在那条线上: fos = new FileOutputStream(file);
如果我将文件名设置为:"MyApp/MediaTag-"+objectId+"它正在工作,但如果我尝试创建并将文件保存到另一个目录,则会抛出异常.那么任何想法我做错了什么?
还有一个问题:有没有办法让我的文件在外部存储中保密,这样用户就无法在图库中看到它们,只有当他连接他的设备时Disk Drive?
我创建了一个位图,现在我想将该位图保存到某个目录.任何人都可以告诉我这是如何完成的.谢谢
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/mnt/sdcard/dcim/Camera/2010-11-16_18-57-18_989.jpg");
buf = new BufferedInputStream(in);
Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);
int oldWidth = _bitmapPreScale.getWidth();
int oldHeight = _bitmapPreScale.getHeight();
int newWidth = 2592;
int newHeight = 1936;
float scaleWidth = ((float) newWidth) / oldWidth;
float scaleHeight = ((float) newHeight) / oldHeight;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0, oldWidth, oldHeight, matrix, true);
Run Code Online (Sandbox Code Playgroud)
(我想将_bitmapScaled保存到SD卡上的文件夹中)