Android 将文件保存到外部存储
我在 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();
但它引发了异常:
java.io.FileNotFoundException:/mnt/sdcard/MyApp/MediaCard/MediaCard-0.png(没有这样的文件或目录)
在该行: fos = new FileOutputStream(file);
如果我设置filename 为: "MyApp/MediaTag-"+objectId+"
它正在工作,但是如果我尝试创建文件并将其保存到另一个目录,则会抛出异常。那么你知道我做错了什么吗?
还有另一个问题:有什么方法可以使我的文件在外部存储中保密,这样用户就无法在图库中看到它们,只有当他将设备连接为磁盘驱动器时代码>?
I have a little issue with creating a directory and saving a file to it on my android application. I'm using this piece of code to do this :
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();
But it's throwing an exception :
java.io.FileNotFoundException: /mnt/sdcard/MyApp/MediaCard/MediaCard-0.png (No such file or directory)
on that line : fos = new FileOutputStream(file);
If I set the filename to : "MyApp/MediaTag-"+objectId+"
it's working, but If I try to create and save the file to an another directory it's throwing the exception. So any ideas what I'm doing wrong?
And another question: Is there any way to make my files private in external storage so user can't see them in gallery, only if he connect his device as Disk Drive
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用此功能将位图保存在 SD 卡中
,并将其添加到清单中
编辑:通过使用此行,您将能够在图库视图中看到保存的图像。
也看看这个链接 http://rajareddypolam.wordpress.com/?p=3&预览=真
Use this function to save your bitmap in SD card
and add this in manifest
EDIT: By using this line you will be able to see saved images in the gallery view.
look at this link also http://rajareddypolam.wordpress.com/?p=3&preview=true
RajaReddy 提供的代码不再适用于 KitKat
这个代码可以(2 处更改):
The code presented by RajaReddy no longer works for KitKat
This one does (2 changes):
更新 2018,SDK >= 23。
现在您还应该检查用户是否已授予外部存储权限,方法是:
当然,在
AndroidManifest.xml
中添加:Update 2018, SDK >= 23.
Now you should also check if the user has granted permission to external storage by using:
and of course, add in the
AndroidManifest.xml
:的许可:
您需要获得此方法
You need a permission for this
and method:
确保您的应用程序具有适当的权限,可以写入外部存储: http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE
它在你的清单文件中应该看起来像这样:
Make sure your app has the proper permissions to be allowed to write to external storage: http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE
It should look something like this in your manifest file:
试试这个:
Try This :
我创建了一个用于保存位图的 AsyncTask。
I have created an AsyncTask for saving bitmaps.
从 android10 开始,旧的保存文件方式可能不适用于新版本的 android。
参考 - https://www.simplifiedcoding.net/android-save-bitmap -到画廊/
Old way of saving files might not work with new versions of android, starting with android10.
Reference- https://www.simplifiedcoding.net/android-save-bitmap-to-gallery/
可能会抛出异常,因为没有
MediaCard
子目录。您应该检查路径中的所有目录是否存在。关于文件的可见性:如果您将名为
.nomedia
的文件放入目录中,您就是在告诉 Android 您不希望它扫描其中的媒体文件,并且它们不会出现在图库中。Probably exception is thrown because there is no
MediaCard
subdir. You should check if all dirs in the path exist.About visibility of your files: if you put file named
.nomedia
in your dir you are telling Android that you don't want it to scan it for media files and they will not appear in the gallery.对于 API 级别 23 (Marshmallow) 及更高版本,除了清单中的使用权限之外,还应该实现弹出权限,并且用户需要在运行时使用应用程序时授予它。
下面,有一个示例将
hello world!
保存为图片目录中Test
目录中myFile.txt
文件的内容。在清单中:
您要创建文件的位置:
For API level 23 (Marshmallow) and later, additional to uses-permission in manifest, pop up permission should also be implemented, and user needs to grant it while using the app in run-time.
Below, there is an example to save
hello world!
as content ofmyFile.txt
file inTest
directory inside picture directory.In the manifest:
Where you want to create the file:
自 android 4.4 文件保存已更改。它
返回一个数组。
当 name 为 null 时,
第一个值类似于 /storage/emulated/0/Android/com.my.package/files
第二个值类似于
/storage/extSdCard/Android/com.my.package/files
单个项目数组部分,但它演示了它是如何工作的:
android 4.3 及以下版本它重新调整了一些杂乱代码的
since android 4.4 file saving has been changed. there is
it retuns an array.
when name is null
the first value is like /storage/emulated/0/Android/com.my.package/files
the second value is like
/storage/extSdCard/Android/com.my.package/files
android 4.3 and less it retuns a single item array
parts of little messy code but it demonstrates how it works:
这段代码工作得很好&也曾参与 KitKat 的工作。欣赏@RajaReddy PolamReddy
此处添加了更多步骤,并且在图库上也可见。
这是画廊中可见的媒体扫描仪。
This code is Working great & Worked on KitKat as well. Appreciate @RajaReddy PolamReddy
Added few more steps here and also Visible on Gallery as well.
This is Media scanner to Visible in Gallery.
点击此处查看完整说明和源代码
Click Here for full description and source code