简体   繁体   English

获取存储在android的res / raw文件夹中的.mp3文件的URI

[英]Get URI of .mp3 file stored in res/raw folder in android

I have many .mp3 files stored in res/raw folder. 我在res / raw文件夹中存储了许多.mp3文件。

I am getting URI of .mp3 file using following code. 我正在使用以下代码获取.mp3文件的URI。

Uri.parse("android.resource:///com.my.android.sharesound/"+resId);

This returns : android.resource:///com.my.android.sharesound/2130968609 这将返回: android.resource:///com.my.android.sharesound/2130968609

Now I am using this URI in creating Share Intent 现在,我正在使用此URI创建共享意图

    shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.my.android.sharesound/"+resId));
    startActivity(Intent.createChooser(shareIntent,"Share Sound");

When i select any Mail application eg. 当我选择任何邮件应用程序例如。 Gmail or YahooMail from the Share Intent, the mp3 file attached successfully. 来自Gmail或YahooMail的共享意向,成功附加了mp3文件。 But it shows that 2130968609(as an attachment) 但它显示2130968609(作为附件)

i dont want resourceId(2130968609) to appear. 我不希望resourceId(2130968609)出现。

I want to display fileName.mp3 我想显示fileName.mp3

How can i do that? 我怎样才能做到这一点? Am i missing something ? 我想念什么吗?

OR 要么

Is there any other way to attach .mp3 file stored in res/raw to mail via Share Intent. 是否有其他方法可以将通过res / raw存储的.mp3文件通过Share Intent附加到邮件。

Finally after searching a lot i found the solution. 最终,经过大量搜索,我找到了解决方案。

If you want to get any resource URI then there are two ways : 如果要获取任何资源URI,则有两种方法:

  1. Using Resource Name 使用资源名称

    Syntax : android.resource://[package]/[res type]/[res name] 语法: android.resource://[package]/[res type]/[res name]

    Example : Uri.parse("android.resource://com.my.package/drawable/icon"); 示例: Uri.parse("android.resource://com.my.package/drawable/icon");

  2. Using Resource Id 使用资源ID

    Syntax : android.resource://[package]/[resource_id] 语法: android.resource://[package]/[resource_id]

    Example : Uri.parse("android.resource://com.my.package/" + R.drawable.icon); 示例: Uri.parse("android.resource://com.my.package/" + R.drawable.icon);

This were the examples to get the URI of any image file stored in drawable folder. 这是获取存储在drawable文件夹中的任何图像文件的URI的示例。

Similarly you can get URIs of res/raw folder. 同样,您可以获取res / raw文件夹的URI。

In my case i used 1st way ie. 就我而言,我使用第一种方法,即。 Using Resource Name 使用资源名称

This work like magic: Assuming the mp3 is saved in the raw folder as 就像魔术一样工作:假设mp3被保存为原始文件夹

notification_sound.mp3 notification_sound.mp3

then simple doing this works: 然后简单地做到这一点:

Uri uri=Uri.parse("android.resource://"+getPackageName()+"/raw/notification_sound");

Here are some methods that might help someone: 以下是一些可以帮助某人的方法:

    public Uri getRawUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/raw/" + filename);
    }
    public Uri getDrawableUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/drawable/" + filename);
    }
    public Uri getMipmapUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/mipmap/" + filename);
    }

Just call the method like this: 只需这样调用方法:

Uri rawUri = getRawUri("myFile.filetype");

一行回答

RingtoneManager.getRingtone(getContext(),Uri.parse("android.resource://com.my.package/" + R.raw.fileName)).play();

Try saving the file to your SDCard and maybe then send the new URI, 尝试将文件保存到SDCard,然后发送新的URI,

 public boolean saveAs(int resSoundId){
 byte[] buffer = null;
 InputStream fIn = getBaseContext().getResources().openRawResource(resSoundId);
 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 = "/sdcard/yourapp/temp/";
 String filename = "filename"+".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;
 }    

Remember to delete the file after the email is sent successfully. 电子邮件发送成功后,请记住删除该文件。

这是获取任何资源的一种干净方法,

 Uri.parse(String.format("android.resource://%s/%s/%s",this.getPackageName(),"resource_folder_name","file_name"));

Don't construct string on your own, use Uri.Builder : 不要自己构造字符串,请使用Uri.Builder

/**
 * @param resourceId identifies an application resource
 * @return the Uri by which the application resource is accessed
 */
internal fun Context.getResourceUri(@AnyRes resourceId: Int): Uri = Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(packageName)
    .path(resourceId.toString())
    .build()

From AOSP-DeskClock. 从AOSP-DeskClock。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM