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

不打扰是莪最后的温柔 提交于 2019-12-13 05:48:51

问题


I want to save a simple .txt file in Android and later I want to copy this file from the device to a PC where the device is mounted as a MTP-Device.

I own two Android Devices:

  • Nexus 4, Stock Android 5.0.1
  • Nexus 7 2012, CyanogenMod 12, Android 5.0.2

To make sure that it's not a Nexus device bug I tried a Wiko phone from a friend.

I use this code to save the file to the Downloads folder on the external storage. This is recommended on the 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 successfully 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 the following permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

When I use MTP to connect to the device I get an empty download folder but when I access the folder through a File Browser in Android (in my case it's ES File Browser) I can see the file and the content. So I think creating the file and writing to it works.

When creating the file I receive the right Log: "File successfully created."


回答1:


You need to add the file to the MediaStore: (quite a 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 for all files

            mMs.disconnect();
        }

        @Override
        public void onScanCompleted(String path, Uri uri) {
        }

    }.init();
} catch(Exception e) {
    // Device does not support adding files manually. 
    // Sending Broadcast to start MediaScanner (slower than adding manually)
    try {   
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    } catch(Exception ee) {
        // Something went terribly wrong. 
        // Can't add file to MediaStore and can't send Broadcast to start MediaScanner.
    }
}


来源:https://stackoverflow.com/questions/29938496/cant-access-stored-files-in-public-folder-in-android-mtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!