![](/img/trans.png)
[英]Set profile picture and save in internal memory folder-android app
[英]The folder is not created in the internal memory on android 10
我的具有所有必要權限的代碼不會在 android 10 上創建文件夾(寫:“創建文件夾失敗”)。 這可能是什么原因造成的? 請幫助我 - 不太了解我的代碼中的錯誤。
活動(如果你需要代碼的rest,我可以扔掉它):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (hasPermissions()){
makeFolder();
}
else {
method for requesting permission to work with files from the user();
}
});
}
private void makeFolder(){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"Folder");
if (!file.exists()){
Boolean ff = file.mkdir();
if (ff){
Toast.makeText(MainActivity.this, "Folder created successfully", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Failed to create folder", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(MainActivity.this, "Folder already exist", Toast.LENGTH_SHORT).show();
}
}
顯現:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
讓我們簡單點。 這是創建文件夾的方法調用:
makeFolder ( getFolderPath() );
獲取文件夾路徑的方法:
public String getFolderPath()
{
String path = new File(Environment.getExternalStorageDirectory() + "/New Folder").getPath();
return path;
}
現在創建文件夾的主要方法:
private void makeFolder(String folderPath)
{
File file = new File(folderPath);
if (!file.exists()) {
file.mkdir();
Log.i("Folder path: " , " " + folderPath );
} else {
Log.e("Folder not created" , "Already exists!")
}
}
將file.mkdir()
替換為file.mkdirs()
,這應該可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.