0

I have a really big problem in getting address from asset folder when I'm using the default music player as an intent! This is my code:

Uri path = Uri.parse("file:///android_asset/file.mp3");
String newPath = path.toString();
Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
File file = new File(newPath);  
intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
startActivity(intent);

The App just works fine on my device but when I wanna run the intent, a TOAST just say: " Unable to find item " What should I do?

Amin
  • 16
  • 3

1 Answers1

1

Third party apps cannot respond to file:///android_asset Uri values, as they will attempt to pull that asset from their app, not yours, and they do not have that asset.

You can copy the asset to internal storage and serve it to third-party apps using FileProvider. Or, I have a StreamProvider that does the same thing but serves the data straight out of the assets area, avoiding the local file copy.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can I just do my way with RAW folder??? I mean can you just re-write my code using raw folder Uri? – Amin Feb 15 '15 at 15:15
  • @Amin: There are very few media players that support playing raw resources from other apps. – CommonsWare Feb 15 '15 at 15:17
  • @CommomsWare I just need to launch with device default player! Is it possible? – Amin Feb 15 '15 at 15:21
  • @Amin: First, there are *thousands* of Android devices, with *hundreds* of "device default player" apps. Second, the user is welcome to use any media player they want to handle your `startActivity()` call, if there is more than one media player on the device, such as ones installed from the Play Store. None of them have to support playing raw resources from your app. – CommonsWare Feb 15 '15 at 15:23
  • @CommomsWare How can I use your StreamProvider on Eclipse? – Amin Feb 18 '15 at 07:30
  • @Amin: Quoting the documentation, "This Android library project is available [as a JAR](https://github.com/commonsguy/cwac-provider/releases) or as an artifact for use with Gradle". Download the JAR and use it as you would any other JAR in an Android Eclipse project (put the JAR in `libs/` and just start using it). – CommonsWare Feb 20 '15 at 23:40