无法访问存储的文件公用文件夹中的Andr​​oid

无法访问存储的文件公用文件夹中的Andr​​oid

本文介绍了无法访问存储的文件公用文件夹中的Andr​​oid(MTP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保存在Android的一个简单的 .TXT 文件后,我想将这个文件从设备复制到设备安装为MTP-电脑设备。

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.

我自己的两款Android设备:

I own two Android Devices:


  • 的Nexus 4,股票的Andr​​oid 5.0.1

  • 的Nexus 7 2012年,12的CyanogenMod的Andr​​oid 5.0.2

要确保这不是我尝试了永光电话从朋友的Nexus设备的bug。

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

我用这个code将文件下载文件夹保存在外部存储。这是建议开发者页面上。

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"/>

当我使用MTP连接到该设备,我得到一个空的下载文件夹,但是当我通过文件浏览器访问该文件夹中的Andr​​oid(在我的情况下,它是ES文件浏览器)我可以看到文件和内容。所以我觉得创建文件,并写入它的工作原理。

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."

推荐答案

您需要将文件添加到 MediaStore :(颇多code的。 ..)

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.
    }
}

这篇关于无法访问存储的文件公用文件夹中的Andr​​oid(MTP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 16:02