不要硬编码"/ sdcard /"; 媒体存储变化?

vam*_*x6x 1 eclipse sdk android

截至目前我正在使用这个:

String path="/sdcard/media/audio/ringtones/";
               String filename=soundname+".ogg";

               boolean exists = (new File(path)).exists();
               if (!exists){new File(path).mkdirs();}

               FileOutputStream save;
               try {
               save = new FileOutputStream(path+filename);
               save.write(buffer);
               save.flush();
               save.close();
               } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               return false;
               } catch (IOException e) {
               // TODO Auto-generated catch block
               return false;
               }

               sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

               File k = new File(path, filename);

               ContentValues values = new ContentValues();
               values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
               values.put(MediaStore.MediaColumns.TITLE, soundname);
               values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
               values.put(MediaStore.Audio.Media.ARTIST, "...");
               values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
               values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
               values.put(MediaStore.Audio.Media.IS_ALARM, true);
               values.put(MediaStore.Audio.Media.IS_MUSIC, false);

               //Insert it into the database
              this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);


              String i = "Saved as Ringtone.";
               Toast.makeText(getApplicationContext(), i,
               Toast.LENGTH_LONG).show();

             return true;


              }
Run Code Online (Sandbox Code Playgroud)

但是eclipse说我应该使用:

Environment.getExternalStorageDirectory().getPath()
Run Code Online (Sandbox Code Playgroud)

我尝试添加它,但现在它不会保存,我不知道该怎么做.有人可以帮我实现吗?

The*_*dak 6

没关系.

但是你可以添加这一行:

@SuppressLint("SdCardPath")
Run Code Online (Sandbox Code Playgroud)

以上

String path="/sdcard/media/audio/ringtones/";
Run Code Online (Sandbox Code Playgroud)

(或)改变

String path="/sdcard/media/audio/ringtones/";
Run Code Online (Sandbox Code Playgroud)

至:

String path=android.os.Environment.getExternalStorageDirectory().getPath() + "/media/audio/ringtones/";
Run Code Online (Sandbox Code Playgroud)