Autore Topic: Come ottengo l'URI di un file audio da impostare come suoneria?  (Letto 415 volte)

Offline theKuston

  • Nuovo arrivato
  • *
  • Post: 48
  • Respect: 0
    • Google+
    • Mostra profilo
  • Dispositivo Android:
    Google Nexus 5
  • Play Store ID:
    K-soft
  • Sistema operativo:
    windows 8.1, Ubuntu 14
Come ottengo l'URI di un file audio da impostare come suoneria?
« il: 14 Dicembre 2014, 20:59:17 CET »
0
weeeee raga!
Allora non so come fare a prendere l'uri di un audio da impostare come suoneria della mia app sveglia.
Questo codice serve per prendere l'URI:
Codice (Java): [Seleziona]
public void choseMusicFile(){
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i,1);
    }
con il suo onActivityResult:
Codice (Java): [Seleziona]
@Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        if(requestCode == 1){
            if(resultCode == RESULT_OK){
                Uri uriSound=data.getData();
                SharedPreferences uriPrefs = getSharedPreferences(URI_PREFERENCES,MODE_PRIVATE);
                uriPrefs.edit().putString(URI_KEY,uriSound.toString()).apply();
                Log.e("URI",uriSound+"---"+uriSound.toString());
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

e questo é quello per recuperarlo:
Codice (Java): [Seleziona]
SharedPreferences uriPrefs = getSharedPreferences(URI_PREFERENCES,MODE_PRIVATE);
        String uri = uriPrefs.getString(URI_KEY,Settings.System.DEFAULT_RINGTONE_URI.toString());
        mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(uri));
        Log.e("URI",Uri.parse(uri)+"---"+uri);
        mediaPlayer.start();
E questo il mio LogCat:
Codice: [Seleziona]
Process: k.soft.aiad, PID: 31861
    java.lang.RuntimeException: Unable to start activity ComponentInfo{k.soft.aiad/k.soft.aiad.Unlock}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
            at k.soft.aiad.Unlock.onCreate(Unlock.java:72)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-15 17:37:10.594  32670-32670/k.soft.aiad E/text﹕ 15 / 12 / 2014  17 : 33
12-15 17:37:10.595  32670-32670/k.soft.aiad E/actualCal-alarmCal﹕ 1418661430594 - 1418661180000
12-15 17:37:26.930  32670-32670/k.soft.aiad E/requestCode﹕ 225
12-15 17:37:26.930  32670-32670/k.soft.aiad E/time﹕ 15 / 12 / 2014  17 : 38
12-15 17:37:31.897  32670-32670/k.soft.aiad E/URI﹕ content://media/external/audio/media/525---content://media/external/audio/media/525
12-15 17:37:31.906  32670-32670/k.soft.aiad E/text﹕ 15 / 12 / 2014  17 : 38
12-15 17:37:31.919  32670-32670/k.soft.aiad E/text﹕ 15 / 12 / 2014  17 : 38
12-15 17:38:00.220  32670-32670/k.soft.aiad E/MediaPlayer﹕ Unable to create media player
12-15 17:38:00.221  32670-32670/k.soft.aiad E/URI﹕ content://media/external/audio/media/525---content://media/external/audio/media/525
12-15 17:38:00.222  32670-32670/k.soft.aiad E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: k.soft.aiad, PID: 32670
    java.lang.RuntimeException: Unable to start activity ComponentInfo{k.soft.aiad/k.soft.aiad.Unlock}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
            at k.soft.aiad.Unlock.onCreate(Unlock.java:74)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

(Unlock.onCreate(Unlock.java:72) sarebbe la riga del
Codice (Java): [Seleziona]
mediaPlayer.start();)
Grazie infinite anticipatamente :D
« Ultima modifica: 15 Dicembre 2014, 18:04:26 CET da theKuston »
Tutte le mie applicazioni sul play store:
https://play.google.com/store/apps/developer?id=K-soft