设置铃声类不执行任何操作

发布于 2024-11-26 10:36:09 字数 3227 浏览 1 评论 0原文

编辑:

整个SetRingtone.java——

public class SetRingtone extends Activity{

String TAG = "CFFS"; // Class var for logging - identifies the app in the logcat

public boolean saveas(int ressound){  
      byte[] buffer=null;  
      InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
      int size=0;  

      try {  
       size = fIn.available();  
       buffer = new byte[size];  
       fIn.read(buffer);
       fIn.close();  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }  

      String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone/ringtone.mp3";
      String filename="College Football Fight Song"+".mp3";  


      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, "College Football Fight Song");
     values.put(MediaStore.MediaColumns.SIZE, 215454);
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
     values.put(MediaStore.Audio.Media.ARTIST, "");
     values.put(MediaStore.Audio.Media.DURATION, 230);
     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, false);

     //Insert it into the database  

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

     getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

     Uri newUri = getContentResolver().insert(uri, values);

     RingtoneManager.setActualDefaultRingtoneUri(
             SetRingtone.this,
       RingtoneManager.TYPE_RINGTONE,
       newUri
     );

    return false;
}

我想在其中设置铃声的Java——

private OnLongClickListener onLongImageClick = new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.boston_college_imageview) {
            SetRingtone(R.raw.acc_boston_college);
        }
        return false;
        }
};

以及我将其传递给SetRingtone.java的地方——

private void SetRingtone(int soundID) {
    Intent otherIntent = new Intent();
    otherIntent.setClassName("com.carboni.fightsongs", "com.carboni.fightsongs.SetRingtone");
    otherIntent.putExtra("com.carboni.fightsongs.FILE_RES_ID", soundID);
    startActivity(otherIntent);
}

EDIT:

The entire SetRingtone.java --

public class SetRingtone extends Activity{

String TAG = "CFFS"; // Class var for logging - identifies the app in the logcat

public boolean saveas(int ressound){  
      byte[] buffer=null;  
      InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
      int size=0;  

      try {  
       size = fIn.available();  
       buffer = new byte[size];  
       fIn.read(buffer);
       fIn.close();  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }  

      String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone/ringtone.mp3";
      String filename="College Football Fight Song"+".mp3";  


      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, "College Football Fight Song");
     values.put(MediaStore.MediaColumns.SIZE, 215454);
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
     values.put(MediaStore.Audio.Media.ARTIST, "");
     values.put(MediaStore.Audio.Media.DURATION, 230);
     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, false);

     //Insert it into the database  

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

     getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

     Uri newUri = getContentResolver().insert(uri, values);

     RingtoneManager.setActualDefaultRingtoneUri(
             SetRingtone.this,
       RingtoneManager.TYPE_RINGTONE,
       newUri
     );

    return false;
}

Java where I want to set ringtone --

private OnLongClickListener onLongImageClick = new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.boston_college_imageview) {
            SetRingtone(R.raw.acc_boston_college);
        }
        return false;
        }
};

and where I pass it off to SetRingtone.java --

private void SetRingtone(int soundID) {
    Intent otherIntent = new Intent();
    otherIntent.setClassName("com.carboni.fightsongs", "com.carboni.fightsongs.SetRingtone");
    otherIntent.putExtra("com.carboni.fightsongs.FILE_RES_ID", soundID);
    startActivity(otherIntent);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百思不得你姐 2024-12-03 10:36:09

这就是 ImageView 长按的样子:

imageView1.setOnLongClickListener(new ImageView.OnLongClickListener()
{
    public boolean onLongClick(View v)
    {
        if (v.getId() == R.id.boston_college_imageview)
        {
            SetRingtone(R.raw.acc_boston_college);
        }
        return false;
    }
});

更新:

String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone";
String filename="College Football Fight Song.mp3";  

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

FileOutputStream save;
try
{
    File file = new File(path, filename);
    save = new FileOutputStream(file);
    save.write(buffer);
    save.flush();
    save.close();
}
catch (FileNotFoundException e)
{
    return false;  
}
catch (IOException e)
{
    return false;  
}

This is what the long click should look like with an ImageView:

imageView1.setOnLongClickListener(new ImageView.OnLongClickListener()
{
    public boolean onLongClick(View v)
    {
        if (v.getId() == R.id.boston_college_imageview)
        {
            SetRingtone(R.raw.acc_boston_college);
        }
        return false;
    }
});

UPDATE:

String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone";
String filename="College Football Fight Song.mp3";  

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

FileOutputStream save;
try
{
    File file = new File(path, filename);
    save = new FileOutputStream(file);
    save.write(buffer);
    save.flush();
    save.close();
}
catch (FileNotFoundException e)
{
    return false;  
}
catch (IOException e)
{
    return false;  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文