4
votes

I want to do the following link: http://imagizer.imageshack.us/v2/800x600q90/42/gbs4.jpg

But when I press the ImageButton, I want the sound to be put as a ringtone.

Here is my code:


    public class ListViewAdapter extends BaseAdapter {

        private Context context;
        private List items;
        private LayoutInflater inflater;
        String path="sdcard/media/ringtone";

        public ListViewAdapter(Context context,
                               List items ) {
            inflater = LayoutInflater.from( context );
            this.context = context;
            this.items = items;
        }

        public int getCount() {
            return items.size();
        }

        public Object getItem(int position) {
            return items.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            String item = items.get(position);
            View v = null;
            if( convertView != null )
                v = convertView;
            else
                v = inflater.inflate( R.layout.list_row, parent, false);
            TextView itemTV = (TextView)v.findViewById( R.id.itemText);
            itemTV.setText( item );
            ImageButton button =
                    (ImageButton)v.findViewById( R.id.button);
            button.setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            Toast.makeText( context,
                                    "ImageButton clicked",
                                    Toast.LENGTH_SHORT).show();

                            File k = new File(path, "my sound");

                            ContentValues values = new ContentValues();
                            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                            values.put(MediaStore.MediaColumns.TITLE, "exampletitle");
                            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                            values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
                            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
                           //Insert it into the database
                        Uri newUri= context.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

                        RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
                    });
            return v;
        }
    }

This is my MainActivity code that has no errors:


    public class MainActivity extends Activity {

        MediaPlayer mp = new MediaPlayer();
        Button stop;
        ListViewAdapter adapter;
        int s1[] =
                {
                        R.raw.el_tri_abuelita_soy_tu_nieto,
                        R.raw.el_tri_ahi_te_lo_lavas,
                        R.raw.el_tri_bajate_del_avion,

                };

        ImageButton imageButonMsj, imageButtonPhone;
        int position;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ListView list30 = (ListView) findViewById(R.id.list);

            ArrayList items = new ArrayList();
            items.add("Abuelita soy tu nieto");
            items.add("Ahi te lo lavas");
            items.add("Bajate del avion");


            adapter = new ListViewAdapter(this, items );
            list30.setAdapter(adapter);

            list30.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    method(position);
                    }
                });
            Log.i("ramiro", "llego al final");
        }

     public void method(int position){
            if((mp.isPlaying()==true) && (this.position == position))
                mp.stop();
            else{
                this.position = position;
                try{
                    mp.reset();                               //resets the media player
                    mp.release();                             //release the media player of current audio playing
                    mp=MediaPlayer.create(this,s1[position]); //create a new  media player with the selected id
                    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    //mp.prepare(); //salta exception
                    mp.seekTo(0);                             //seek to starting of song means time=0 ms
                    mp.start();                               //start media player
                }
                catch (Exception e)
                {
                    Toast.makeText(getApplication(), "error exception", Toast.LENGTH_SHORT).show();
                }
            }
        }

I edited and now the application compiles, but when I press the ImageButton, the application sent a message -> Unfortunately, El Tri Sonidos has stopped.


    FATAL EXCEPTION: main
        java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=690, uid=10052 requires android.permission.WRITE_SETTINGS, or grantUriPermission()
                at android.os.Parcel.readException(Parcel.java:1425)
                at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
                at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
                at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
                at android.content.ContentResolver.insert(ContentResolver.java:864)
                at android.provider.Settings$NameValueTable.putString(Settings.java:665)
                at android.provider.Settings$System.putString(Settings.java:862)
                at android.media.RingtoneManager.setActualDefaultRingtoneUri(RingtoneManager.java:655)
                at com.institutovilma.eltri.ListViewAdapterImageButton$1.onClick(ListViewAdapterImageButton.java:80)
                at android.view.View.performClick(View.java:4084)
                at android.view.View$PerformClick.run(View.java:16966)
                at android.os.Handler.handleCallback(Handler.java:615)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4745)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                at dalvik.system.NativeStart.main(Native Method)

How would I fix this runtime error?

4

4 Answers

2
votes

Your IDE is already giving you the right hints. Change your buggy lines to match these:

RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);

and

Uri newUri= context.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

Notice you have to pass context in both cases.

2
votes

You should be add permission on AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
0
votes

inside the onClickListener, this refers to the button,and you cannot call contentResolver on a button, you need a context for this. you can try :

getApplicationContext().getContentResolver()
0
votes

Scope storage starting from API29 you can't have write permission if you don't really need it. There are many ways to do this.

  • At API29 level you need to add the flag "android:requestLegacyExternalStorage="true" and ask for write permission as usual.
  • Above level 29 you need to add permission to be able to read and write memory.
  • If you are below API level 29 then you should find a way to request permission write storage with specific instructions and sample here "https://stackguides.com/questions/33162152/storage-permission-error-in-marshmallow"