storage - Can't access stored files in public folder in Android (MTP) -


i want save simple .txt file in android , later want copy file device pc device mounted mtp-device.

i own 2 android devices:

  • nexus 4, stock android 5.0.1
  • nexus 7 2012, cyanogenmod 12, android 5.0.2

to make sure it's not nexus device bug tried wiko phone friend.

i use code save file downloads folder on external storage. recommended on developer page.

private void savedata()     {             string filename = "test.txt";         string writestring = "hello world";         file filepath = environment.getexternalstoragepublicdirectory(environment.directory_downloads);         file savefile = new file(filepath, filename);         savefile.setreadable(true);          try         {             boolean result = savefile.createnewfile();             if(result == true)             {                 log.i(tag, "file created");             }             else             {                 log.i(tag, "error. file not created");             }             bufferedwriter writer = new bufferedwriter(new filewriter(savefile));             writer.write(writestring);             writer.close();         }         catch(exception e)         {             log.e(tag,e.tostring());         }     } 

i use following permissions:

<uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="android.permission.read_external_storage"/> 

when use mtp connect device empty download folder when access folder through file browser in android (in case it's es file browser) can see file , content. think creating file , writing works.

when creating file receive right log: "file created."

you need add file mediastore: (quite lot of code...)

try {     new mediascannerconnectionclient() {         private mediascannerconnection mms;          public void init() {             mms = new mediascannerconnection(mycontext, this);             mms.connect();         }          @override         public void onmediascannerconnected() {              file pathfile = environment                    .getexternalstoragepublicdirectory(environment.directory_downloads);             file scanfile = new file(pathfile, "test.txt");              mms.scanfile(scanfile.getabsolutepath(), null); // <-- repeat files              mms.disconnect();         }          @override         public void onscancompleted(string path, uri uri) {         }      }.init(); } catch(exception e) {     // device not support adding files manually.      // sending broadcast start mediascanner (slower adding manually)     try {            sendbroadcast(new intent(intent.action_media_mounted,                  uri.parse("file://" + environment.getexternalstoragedirectory())));     } catch(exception ee) {         // went terribly wrong.          // can't add file mediastore , can't send broadcast start mediascanner.     } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -