Android에서 SDCard에서 폴더를 만드는 방법
문제
SDCard에서 폴더를 만들고 싶고 아래 코드를 사용했습니다 :
public class Screen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
operateOnFirstUsage();
}
private void operateOnFirstUsage() {
String state = Environment.getExternalStorageState();
Log.d("Media State", state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
File appDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/");
Log.d("appDirectroyExist", appDirectory.exists() + "");
if (!appDirectory.exists())
Log.d("appDir created: ", appDirectory.mkdir() + "");
File dbDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Database/");
Log.d("dbDirectroyExist", dbDirectory.exists() + "");
if (!dbDirectory.exists())
Log.d("dbDir created: ", dbDirectory.mkdirs() + "");
File themesDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Themes/");
Log.d("themesDirectroyExist", themesDirectory.exists() + "");
if (!themesDirectory.exists())
Log.d("themesDir created: ", themesDirectory.mkdirs() + "");
}
}
}
.
또한 sdcard 쓰기 권한을 설정했습니다.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
.
응용 프로그램을 여러 번 실행하고 LogCat 출력을 얻을 때마다 :
01-09 21:38:13.701: D/Media State(15363): mounted
01-09 21:38:13.701: D/appDirectroyExist(15363): false
01-09 21:38:13.701: D/appDir created:(15363): false
01-09 21:38:13.701: D/dbDirectroyExist(15363): false
01-09 21:38:13.701: D/dbDir created:(15363): false
01-09 21:38:13.701: D/themesDirectroyExist(15363): false
01-09 21:38:13.701: D/themesDir created:(15363): false
.
나는 유사한 질문을 읽었지만, 얻는 것이 유용하지는 않습니다.코드를 실행하려면 어떻게해야합니까?내 문제는 무엇입니까?
해결책 5
응용 프로그램이 그런 식으로 작동 했는가는 모르겠지만, 마침내 Android 매니페스트 파일에서 SDCard 쓰기 권한을 제거하고 재설정 하여이 문제를 극복합니다.친절한 도움을 청하고 시간을내는 데 사과드립니다.
다른 팁
편집
시도 :
File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/");
if(!mydir.exists())
mydir.mkdirs();
else
Log.d("error", "dir. already exists");
.
및 Rececheck 권한
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
. Pictures 폴더에서 폴더 mydir를 생성하는 방법은 sdcard :
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyDir");
mediaStorageDir.mkdirs();
. 이 방법을 시도하십시오 :
File file=new File(Environment.getExternalStorageDirectory() + File.separator +"MyApp/");
file.mkdirs();
. 이 코드를 시도하십시오 :), 그것의 간단한.
String fileName = "CallHistory.pdf";
// create a File object for the parent directory
File PDF_Directory = new File("/sdcard/MOBstate/");
// have the object build the directory structure, if needed.
PDF_Directory.mkdirs();
// create a File object for the output file
File outputFile = new File(PDF_Directory, fileName);
// now attach the OutputStream to the file object, instead of a ring representation
FileOutputStream fos = new FileOutputStream(outputFile);
. 제휴하지 않습니다 StackOverflow