1

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

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;


              }

但是eclipse说我应该使用:

Environment.getExternalStorageDirectory().getPath()

我试着添加它,但现在它不会保存,我不知道该怎么做。任何人都可以帮我实现这个吗?

4

3 回答 3

6

没关系。

但是你可以添加这一行:

@SuppressLint("SdCardPath")

多于

String path="/sdcard/media/audio/ringtones/";

(或)只是改变

String path="/sdcard/media/audio/ringtones/";

到:

String path=android.os.Environment.getExternalStorageDirectory().getPath() + "/media/audio/ringtones/";
于 2013-08-22T12:11:21.237 回答
1

您可以通过抑制它来避免这种情况:

@SuppressLint("SdCardPath")

但你应该使用Environment.getExternalStorageDirectory()

String PathToSdcard = Evironment.getExternalStorageDirectory().getPath()+"/media/audio/ringtone"
于 2013-08-22T12:21:03.653 回答
0

最好使用Environment,因为您无法确定外部存储路径是"/sdcard/". 尝试使用

File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES)

然后进行一些重构:

if (!dir.exists()) dir.mkdirs();
File file = new File(dir, filename);
save = new FileOutputStream(file);
于 2013-08-22T12:20:58.130 回答