AI.
AI.

Reputation: 996

How to select audio file when editText is clicked?

When editText in fragment class (ReminderPage) is clicked, I want it to open audio file so I can select music file as ringtone.

ringtone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {

                        Intent intent1 = new Intent();
                        intent1.setAction(Intent.ACTION_GET_CONTENT);
                        intent1.setType("audio/*");
                        startActivityForResult(
                                Intent.createChooser(intent1, "Choose Sound File"), 6);
                    }
                });

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data,Context context) {
        if (resultCode == getActivity().RESULT_OK && requestCode == 6) {
            Uri i = data.getData();  // getData
            String s = i.getPath(); // getPath
            File k = new File(s);  // set File from path
            if (s != null) {      // file.exists

                ContentValues values = new ContentValues();
                values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                values.put(MediaStore.MediaColumns.TITLE, "ring");
                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                values.put(MediaStore.MediaColumns.SIZE, k.length());
                values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
                values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                values.put(MediaStore.Audio.Media.IS_ALARM, true);
                values.put(MediaStore.Audio.Media.IS_MUSIC, false);

                Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
                        .getAbsolutePath());
                context.getContentResolver().delete(
                        uri,
                        MediaStore.MediaColumns.DATA + "=\""
                                + k.getAbsolutePath() + "\"", null);
                Toast.makeText(getActivity(),values+"",Toast.LENGTH_LONG).show();
                Uri newUri = context.getContentResolver().insert(uri, values);

                try {
                    RingtoneManager.setActualDefaultRingtoneUri(
                            getActivity().ReminderPage, RingtoneManager.TYPE_RINGTONE,
                            newUri);
                } catch (Throwable t) {

                }
            }
        }
    }

But I get errors and the onActivityResult is never used....

Error:(473, 42) error: cannot find symbol variable ReminderPage Error:(443, 5) error: method does not override or implement a method from a supertype

After I change to getActivity().getContentResolver() and Reminder.this, I get new errors

Error:(444, 20) error: onActivityResult(int,int,Intent) in ReminderPage cannot override onActivityResult(int,int,Intent) in Fragment attempting to assign weaker access privileges; was public Error:(472, 36) error: method setActualDefaultRingtoneUri in class RingtoneManager cannot be applied to given types; required: Context,int,Uri found: ReminderPage,int,Uri reason: actual argument ReminderPage cannot be converted to Context by method invocation conversion

Upvotes: 0

Views: 350

Answers (2)

sdabet
sdabet

Reputation: 18670

For the first error, try to replace getActivity().ReminderPage by getActivity() when calling setActualDefaultRingtoneUri.

Regarding the second error: there is no Context context parameter in the Fragment.onActivityResult method and the visibility should be public. You must respect the signature of the method when overriding it.

Replace

protected void onActivityResult(int requestCode, int resultCode, Intent data,Context context) {

by

public void onActivityResult(int requestCode, int resultCode, Intent data) {

And replace

context.getContentResolver()

by

getActivity().getContentResolver()

Upvotes: 2

petrumo
petrumo

Reputation: 1116

this line could be the problem:

getActivity().ReminderPage

you could use

getActivity()

Upvotes: 1

Related Questions