Android writing file issue while setting song as Ringtone -


i have written program set song ringtone, facing small issue

when tap on button first time set song ringtone, getting this:

enter image description here

but when click on button again (second time), getting this:

enter image description here

so reason ? why never success in first time...

here complete code of ringtoneactivity.java:

    public class ringtoneactivity extends activity {      private final context context = this;     private static final string tag = "meri desi look";     private file sound;     private final file folder = environment.getexternalstoragepublicdirectory(environment.directory_ringtones);     private mediaplayer mediaplayer;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          button buttonringtone = (button) findviewbyid(r.id.btnringtone);         buttonringtone.setonclicklistener(new onclicklistener() {             @override             public void onclick(view v) {                 ask();             }         });     }       private void ask() {         mediaplayer = mediaplayer.create(this, r.raw.meri_desi_look);         mediaplayer.start();         alertdialog.builder alert = new alertdialog.builder(context);         alert.settitle("meri desi look");         alert.setmessage("you want sound ringtone?");         alert.setcancelable(false);         alert.setpositivebutton("yes", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int whichbutton) {                 mediaplayer.stop();                 mediaplayer.reset();                 confirmed();                 dialog.dismiss();             }         });         alert.setnegativebutton("no", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int whichbutton) {                 mediaplayer.stop();                 mediaplayer.reset();                 dialog.dismiss();             }         });         alert.show();     }      /**      * when user confirms popup, run, saving file ,      * setting ringtone utilising ringtonemanager class.      */     private void confirmed() {         boolean success = false;         sound = new file(folder, "meri_desi_look.mp3");         if (!sound.exists()) {             log.i(tag, "writing meri desi " + folder.tostring());             try {                 inputstream in = getresources().openrawresource(r.raw.meri_desi_look);                 fileoutputstream out = new fileoutputstream(sound.getpath());                 byte[] buff = new byte[1024];                 int read = 0;                  try {                     while ((read = in.read(buff)) > 0) {                         out.write(buff, 0, read);                     }                 } {                     in.close();                      out.close();                 }             } catch (exception e) {                 success = false;                 log.i(tag, "desi failed write.");             }         } else {             success = true;             log.i(tag, "meri desi ringtone there.");         }          if (!success) {             onsetringtoneerror("we couldn't give meri desi ringtone.\n\nthere's issue writing file.");         } else {             setringtone();         }     }      /**      * physically sets ringtonemanager preferences.      */     private void setringtone() {         try {             contentvalues values = new contentvalues();             values.put(mediastore.mediacolumns.data, sound.getabsolutepath());             values.put(mediastore.mediacolumns.title, "meri desi - performed sunny leone");             values.put(mediastore.mediacolumns.mime_type, "audio/*");             values.put(mediastore.audio.media.artist, "meri desi look");             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, true);              uri uri = mediastore.audio.media.getcontenturiforpath(sound.getabsolutepath());             getcontentresolver().delete(uri, mediastore.mediacolumns.data + "=\"" + sound.getabsolutepath() + "\"",                     null);             uri newuri = getcontentresolver().insert(uri, values);              ringtonemanager.setactualdefaultringtoneuri(context, ringtonemanager.type_ringtone, newuri);             onsetringtonesuccess();         } catch (exception e) {             onsetringtoneerror("we couldn't give meri desi ringtone.\n\nthere's issue setting file.");         }     }      /**      * generic helper show error messages (if ever there any) :p      *       * @param message      */     private void onsetringtoneerror(string message) {         alertdialog.builder alert = new alertdialog.builder(context);         alert.settitle("meri desi look");         alert.setmessage(message);         alert.setcancelable(false);         alert.setpositivebutton("ok. that's bad.", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int whichbutton) {                 dialog.dismiss();             }         });         alert.show();     }       private void onsetringtonesuccess() {         alertdialog.builder alert = new alertdialog.builder(context);         alert.settitle("meri desi look");         alert.setmessage("you've got ringtone :)\n\nhope enjoy it!");         alert.setcancelable(false);         alert.setpositivebutton("sweet!", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int whichbutton) {                 dialog.dismiss();             }         });         alert.show();     }      @override     protected void onpause() {         if (mediaplayer.isplaying()) {             mediaplayer.stop();             mediaplayer.release();         }         super.onpause();     }  } 

i think must have figured out long ago issue in code. if didn't,try following.

private void confirmed() {  ...  try {       while ((read = in.read(buff)) > 0) {       out.write(buff, 0, read);     }     success = true; }  catch(exception e) {      success = false;      e.printstacktrace(); } {     n.close();     out.close(); } ...  } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -