我还是Java的初学者,我正在创建一个简单的Android应用程序。为此,我需要在打开应用程序时创建2个文件夹。我创建了一个getter和setter类来传递主类中的文件夹名称参数。但它不起作用。没有任何错误。请帮我解决这个问题。
Getters和setters类
public class CommonClass {
private String folderName;
public CommonConfig(String folderName) {
this.folderName = folderName;
}
public CommonConfig() {
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
}
主类
public class MainActivityClass extends Activity {
private static final String TAG = "svsvsf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CommonClass video = new CommonClass();
video.setFolderName("Video");
CommonClass audio = new CommonClass();
audio.setFolderName("Audio");
TempVideoCreator(video,audio);
}
});
}
public void TempFolderCreator(CommonConfig video,CommonConfig audio){
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + video + audio );
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
Log.d(TAG, "File already created");
} else {
Log.d(TAG, "faileddd");
}
}
答案 0 :(得分:2)
这可能有用。在这种情况下,我假设您想为视频和音频创建单独的文件夹。如果要在视频中创建音频文件夹。在视频和音频之间附加额外的File.seperator。
public void TempFolderCreator(CommonConfig video,CommonConfig audio){
File folderVideo = new File(Environment.getExternalStorageDirectory() +
File.separator + video.getFolderName());
File folderAudio = new File(Environment.getExternalStorageDirectory() +
File.separator + audio.getFolderName());
boolean successVideo = true;
boolean successAudio = true;
if (!folderVideo.exists() ) {
successVideo = folderVideo.mkdirs();
}
if (!folderAudio.exists() ) {
successAudio = folderAudio.mkdirs();
}
//Arrange your error handling logic for Audio folder and video folder
}
答案 1 :(得分:1)
我自己最近努力工作这类代码,因为当你在某些设备上调用Environment.getExternalStorageDirectory()
时,Android会返回奇怪的目录。
在我的设备上,我使用了myApplication.getApplicationContext().getCacheDir()
,但我只需要一个临时目录。
请参阅:Environment.DIRECTORY_DOWNLOADS returns incorrect directory
注意:初看起来,您的代码似乎无法编译。您可能已将其简化为发布,但这无助于了解您的问题。
答案 2 :(得分:1)
让我向你解释一些事情......
在创建new File()
时,我们将路径作为new File("C://hiranya//video//newtextfile.txt")
之类的参数传递。在您的情况下,您将Common
类的对象传递给TempFolderCreator
,并且您直接指定文件中的对象,这是错误的。您在CommonClass
对象中设置了文件夹名称,TempFolderCreator
将类似于
public void TempFolderCreator(CommonConfig video,CommonConfig audio){
File videoFolder = new File(Environment.getExternalStorageDirectory() +
File.separator + video.getFolderName() );
File audioFolder = new File(Environment.getExternalStorageDirectory() +
File.separator + audio.getFolderName() );
boolean success = true;
if (!videoFolder .exists()) {
success = folder.mkdirs();
}
if (!audioFolder .exists()) {
success = folder.mkdirs();
}
//rest of the code
}
答案 3 :(得分:1)
参数传递正常。但这种表达是可疑的:
File.separator + video + audio
这相当于
File.separator + video.toString() + audio.toString()
......这不太可能是你想要的。要获取文件夹名称,请使用getter: video.getFolderName()
还要小心一次创建每个文件夹。
全部放在一起:
private void createFolder(String folderName)
File folder = new File(Environment.getExternalStorageDirectory(), folderName);
boolean success = folder.exists();
if (!success) {
success = folder.mkdirs();
}
if (success) {
Log.d(TAG, "Folder "+folderName+" already created");
} else {
Log.d(TAG, "Creating folder "+folderName+" failed");
}
}
public void TempFolderCreator(CommonConfig video,CommonConfig audio){
createFolder(video.getFolderName());
createFolder(audio.getFolderName());
}