简体   繁体   中英

Android Studio - Acces to /storage/emulated/0 from windows explorer and android file manager

I a newbie in android development. I sure that many people have same issue. I ve a Galaxy S7 phone without SD Card. So no external storage. But i want create file with my app which have to be access from windows explorer to export it.

Note : debbug in USB mode - No virtual device

Of course in my manifest file i ve setted those 2 permissions :

android.permission.WRITE_INTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE

To be sure i ve created a file i use this write method and the following read method :

    File root = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
    File customFolder = new File(root.getAbsolutePath() + "/CustomFolder");
    customFolder.mkdirs();
    file = new File(customFolder, "myData.txt");

    // Must catch FileNotFoundException and IOException
    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Howdy do to you,");
        pw.println("and the horse you rode in on.");
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "File not found");
    } catch (IOException e) {
        e.printStackTrace();
        Log.i(TAG, "I/O exception");
    }

To read myData.txt

try {
BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while (true) {
            line= br.readLine();
            if (line== null) break;
            tv.append("\n" + "    " + line);
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

So all method are OK, but when i browse my files system to get the created file myData.txt, I can't find it !!

Most of the apps we install have their own folder on the root, like Snapchat, Whatsapp etc etc

I d like to make the same thing, what is the way to write file in:

ROOT --> MyApplicationName --> CustomFolder

Thanks for your help :)

I finally found a solution adding a MediaScannerConnection.scanFile() method.

MediaScannerConnection.scanFile(getApplicationContext(),new String[]{file.getAbsolutePath()},null,new MediaScannerConnection.OnScanCompletedListener() {
      @Override
      public void onScanCompleted(String path, Uri uri) {
            // Do something here if you want
      }});

But this solution don't totally fit me, because the storage folder is :

Android/data/donain_name/files/Documents/CustomFolder

I'd like to have same result like whatsapp application which can have a folder in the internal storage root.

The result i want is : MyApplication/CustomeFolder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM