Frage

How can I create folder on Sdcard on android device? I want when I click Set as Ringtone-Create folder and then copy file Also if folder exists then just copy file. So far I've en using this

    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();

But then I noticed the problem, on some devices those folders don't exist and file is not copied. How can I solve this?

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)

UPDATE

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();

            }


            }
        );
War es hilfreich?

Lösung

To create directory in SDCard use mkdirs() method as follows...

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

Andere Tipps

try to use mkdirs()

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

and use permission in your manifest

          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top