【问题标题】:Android set ringtone and notificationAndroid设置铃声和通知
【发布时间】:2012-03-05 09:33:42
【问题描述】:

您好,我无法将声音保存为铃声或通知。 我不断收到我的 Toast - 失败 - 检查你的 SDCard

错在哪里?

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

【问题讨论】:

    标签: java android notifications ringtone


    【解决方案1】:

    问题来了

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

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

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

    希望对你有帮助

    【讨论】:

    • 是的,Environment.getExternalStorageDirectory() 无论如何都会返回 /SDCard 路径,因此请跳过该部分
    • 使用String path=Environment.getExternalStorageDirectory().getPath()+"/yourPackage/com.ring/media"‌​;
    • 现在我有错误:(481):未捕获的处理程序:线程主因未捕获的异常而退出(481):android.content.res.Resources$NotFoundException:资源ID#0x0(481):在android.content.res.Resources.getValue(Resources.java:891) AndroidRuntime(481):在 android.content.res.Resources.openRawResource(Resources.java:816) AndroidRuntime(481):在 android.content.res。 Resources.openRawResource(Resources.java:798) InputStream fIn = getBaseContext().getResources().openRawResource(ressound); (savering(selectedSoundId)){ if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
    • 不要使用默认的 Android 包名称并尝试使用您自己的包/文件夹名称对其进行测试,您当前正在写入的文件夹可能无法访问,而是创建您自己的文件夹并再试一次。
    • 字符串路径 = Environment.getExternalStorageDirectory().getPath() + "/com.ring/‌​media";没用……
    【解决方案2】:

    好的,这里是我目前在我的应用程序中用于将数据存储到我的 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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      相关资源
      最近更新 更多