英文:
Can't create a folder in external storage, Android
问题
我试过很多方法来找到解决方案,但到目前为止一切都失败了。这可能是一些愚蠢的问题。
我试图将照片保存为手机存储中的JPEG格式。甚至创建文件夹这样简单的任务也失败了。下面的代码是我用来创建文件夹的:
private void makeFolder(){
try{
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),getString(R.string.app_name));
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
Toast.makeText(this,"已完成",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"文件夹创建失败",Toast.LENGTH_SHORT).show();
System.out.println(folder.getAbsolutePath());
}
}catch (Exception e){
Toast.makeText(this,"异常",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
我尝试了许多不同的方法,如:
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + getString(R.string.app_name) + File.separator);
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + getString(R.string.app_name);
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
,getString(R.string.app_name));
我尝试了mkdirs
和mkdir
,以及以下路径:
Environment.getExternalStorageDirectory().getAbsolutePath()
getExternalStorageDirectory().getAbsolutePath()
Environment.getExternalStorageDirectory()
getExternalStorageDirectory()
在清单文件中有权限声明并已被接受:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
System.out打印的路径是"/storage/emulated/0/appname"。
我不知道还有什么可以尝试的了。类似的代码在其他应用程序中有效。最低API级别为23。希望能有所帮助。
英文:
I tried a lot to find a solution but everything has failed so far. It's probably something stupid.
I'm trying to save a photo as a jpeg in the storage of the phone. Everything fails even the simple task of making a folder. The code below is what I use to create the folder.
private void makeFolder(){
try{
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),getString(R.string.app_name));
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
Toast.makeText(this,"Done",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"folder_failed",Toast.LENGTH_SHORT).show();
System.out.println(folder.getAbsolutePath());
}
}catch (Exception e){
Toast.makeText(this,"exception",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
I have tried many different ways like:
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + getString(R.string.app_name)+File.separator);
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + getString(R.string.app_name);
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
,getString(R.string.app_name));
I have tried both mkdirs and mkdir and
-Environment.getExternalStorageDirectory().getAbsolutePath()
-getExternalStorageDirectory().getAbsolutePath()
-Environment.getExternalStorageDirectory()
-getExternalStorageDirectory()
The permission exists in the manifest and is accepted.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The path the the System.out prints is "/storage/emulated/0/appname"
I don't know what else to try. Similar code works for other applications. Min API is 23.
Any help is much appreciated.
答案1
得分: 1
我建议您尝试使用Context.getExternalFilesDir()
而不是Environment.getExternalStorageDirectory()
。
英文:
I suggest you to try Context.getExternalFilesDir()
instead of Environment.getExternalStorageDirectory()
答案2
得分: 1
如果您的测试设备运行Android 10/Q或API级别29+:在将数据存储到媒体文件夹方面有一些权限更改。您可以将数据存储到您的应用文件夹中而无需问题,但如果您卸载应用程序,图像也会丢失。
最简单的方法是在清单文件的应用程序部分中添加android:requestLegacyExternalStorage="true",但这更像是一种权宜之计,而且可能不会得到长期支持。
在这里搜索"在Android 10上存储图像",您会找到适合您的正确方法。
英文:
If your test device runs Android 10/Q or API Level 29+: there are a few permission changes with storing data to media folder. You can store your data without problems to your app folder, but if you remove the app the images are gone too.
The easiest way is to add android:requestLegacyExternalStorage="true" in the manifest under application but it is more like a work around and it will probably not be supported for long.
Search for "store image android 10" here and you find the right way for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论