以编程方式创建目录?

时间:2014-03-24 10:55:06

标签: android directory ringtone

如何在Android设备上的Sdcard上创建文件夹? 我想点击Set as Ringtone-Create文件夹然后复制文件 如果文件夹存在,那么只需复制文件。 到目前为止,我已经使用了这个

    private final File rpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES);
 private void s1ring() {

    Boolean success = false;
                rsound = new File(rpath, "Summer melody.mp3");
                if (!rsound.exists()) {

                    try {
                        InputStream in = getResources().openRawResource(R.raw.s1beautiful);
                        FileOutputStream out = new FileOutputStream(rsound.getPath());
                        byte[] buff = new byte[1024];
                        int read = 0;

                        try {
                            while ((read = in.read(buff)) > 0) {
                                out.write(buff, 0, read);
                            }
                        } finally {
                            in.close();
                            out.close();
                        }
                    } catch (Exception e) {
                        success = false;                            
                    }
                } else {
                    success = true;
                    setRingtone();                      
                }
                if (!success) { 
                   setRingtone();

但后来我注意到了这个问题,在某些设备上,这些文件夹不存在且文件未被复制。 我该如何解决这个问题?

logcat的:

03-24 12:19:12.331: W/dalvikvm(26957): threadid=1: thread exiting with uncaught exception (group=0x41c3b8b0)
03-24 12:19:12.331: E/AndroidRuntime(26957): FATAL EXCEPTION: main
03-24 12:19:12.331: E/AndroidRuntime(26957): java.lang.NullPointerException
03-24 12:19:12.331: E/AndroidRuntime(26957):    at gsoft.awesomeringtones.S01$1.setRingtone(S01.java:102)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at gsoft.awesomeringtones.S01$1.s1ring(S01.java:89)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at gsoft.awesomeringtones.S01$1.onClick(S01.java:48)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at android.view.View.performClick(View.java:4421)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at android.view.View$PerformClick.run(View.java:17903)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at android.os.Handler.handleCallback(Handler.java:730)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at android.os.Looper.loop(Looper.java:213)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at android.app.ActivityThread.main(ActivityThread.java:5225)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at java.lang.reflect.Method.invokeNative(Native Method)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at java.lang.reflect.Method.invoke(Method.java:525)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-24 12:19:12.331: E/AndroidRuntime(26957):    at dalvik.system.NativeStart.main(Native Method)

更新

private File rsound;

private void setRingtone() {
                ContentValues values = new ContentValues();
                   values.put(MediaStore.MediaColumns.DATA, rsound.getAbsolutePath());
                   values.put(MediaStore.MediaColumns.TITLE, "Summer melody");
                   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                   values.put(MediaStore.Audio.Media.ARTIST, " ");
                   values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                   values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
                   values.put(MediaStore.Audio.Media.IS_ALARM, false);
                   values.put(MediaStore.Audio.Media.IS_MUSIC, true);

                   Uri uri = MediaStore.Audio.Media.getContentUriForPath(rsound.getAbsolutePath());
                   getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + rsound.getAbsolutePath() + "\"",
                           null);
                   Uri newUri = getContentResolver().insert(uri, values);

                   RingtoneManager.setActualDefaultRingtoneUri(
                           S01.this, RingtoneManager.TYPE_RINGTONE,
                           newUri);
                   Toast.makeText(getApplicationContext(), "Ringtone set successfully",
                           Toast.LENGTH_SHORT).show();

            }


            }
        );

2 个答案:

答案 0 :(得分:3)

要在SDCard中创建目录,请使用mkdirs()方法,如下所示......

File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/folderDir/");
dir.mkdirs();

答案 1 :(得分:0)

尝试使用mkdirs()

            File sdCardDirectory = Environment
                    .getExternalStorageDirectory();
            new File(sdCardDirectory + "/XXX/Ringtone/").mkdirs();
            File song= new File(sdCardDirectory
                    + "/XXX/Ringtone/Sample" + numberOfSongs + ".mp3");

并使用您的清单中的权限

          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />