Android设置铃声和通知

时间:2012-03-05 09:33:42

标签: java android notifications ringtone

您好我将声音保存为铃声或通知。 我一直在设置Toast - 失败 - 检查你的SD卡

错误在哪里?

代码:http://pastebin.com/in5ckvk0

2 个答案:

答案 0 :(得分:0)

问题出在这里

  String path=Environment.getExternalStorageDirectory().getPath()+"/sdcard/media/ringtone/";

Environment.getExternalStorageDirectory()无论如何都会返回/ SDCard路径,使用其他文件夹名称来存储铃声。喜欢/ yourPackage / media而不是/ sdcard / media

因为它将被解释为/SDCard/yourPackage/media/

希望有所帮助

答案 1 :(得分:0)

这里是我目前在我的SDCard存储数据应用程序中使用的示例代码,看看并尝试使用它。

    File newSoundFile;
    ContentResolver mCr;

    Log.e("check this tag","path for SDCard:"+Environment.getExternalStorageDirectory().getAbsolutePath());

    //storing at:  /mnt/sdcard/media 
    File directoryStructure=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Halloween");


    //if directory doesn't exist create a directory if that fails too return false
    if(!(directoryStructure.exists())){
        if(!(directoryStructure.mkdirs())){
            return false;
        }
    }

    /*for(int allRing=0;allRing<ringId.length;allRing++)*/{

        newSoundFile     = new File(directoryStructure, "/"+HalloweenWallpaper.longPressSelected+".mp3");
        Uri mUri = Uri.parse("android.resource://"+getApplication().getPackageName()+"/"+ringId[HalloweenWallpaper.longPressSelected]);
        mCr = HalloweenWallpaper.this.getContentResolver();
        AssetFileDescriptor soundFile;
        try {
            soundFile= mCr.openAssetFileDescriptor(mUri, "r");
        } catch (FileNotFoundException e) {
            soundFile=null;
            Log.e("first check", "here --"+e.getMessage());
            return false;
        }

        try {
            byte[] readData = new byte[1024];
            FileInputStream fis = soundFile.createInputStream();
            FileOutputStream fos = new FileOutputStream(newSoundFile);
            int i = fis.read(readData);

            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
            }

            fos.close();
        } catch (IOException io) {
            Log.e("Second Check", "--"+io.getMessage());
            return false;
        }       

    }


    //STORES SUCCESFULLY