无法通过USB看到Android上添加的文件夹 [英] Folder added on Android is not visible via USB

查看:141
本文介绍了无法通过USB看到Android上添加的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图片保存在Android上的子文件夹中.这是我的一些代码:

I'm trying to save pictures in a subfolder on Android. Here's a bit of my code:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirName");
path.mkdirs();

(我尝试使用getExternalStorageDirectory代替getExternalStoragePublicDirectory,并且使用Pictures文件夹代替DCIM.)

(I've tried getExternalStorageDirectory instead of getExternalStoragePublicDirectory and the Pictures folder instead of DCIM.)

通过USB连接设备时,我添加的任何子文件夹(包括其内容)都不会显示在Windows资源管理器中.不过,它确实显示在Android文件管理器中.

Any subfolder I add, including its contents, don't show up in Windows Explorer when the device is connected via USB. It does show in the Android File Manager, though.

我尝试在新目录的父目录上广播ACTION_MEDIA_MOUNTED意图.没用.

I've tried broadcasting the ACTION_MEDIA_MOUNTED intent on the new directory's parent. It didn't work.

如果我在Windows中添加文件,该文件将显示在Android上.如果我通过文件管理器在Android上添加文件,则该文件会显示在Windows中.如果我以编程方式添加文件,则该文件将显示在Android文件管理器中,但不会显示在Windows资源管理器中.而且,我需要从Windows获取它,而且我不希望最终用户必须手动创建文件夹.

If I add a file in Windows, it shows up on Android. If I add a file on Android via the File Manager, it shows up in Windows. If I add the file programmatically, it shows up on the Android File Manager, but not in Windows Explorer. And I need to get it from Windows, and I don't want the final user to have to create the folder manually.

我在做什么错了?

推荐答案

我也遇到了同样的问题,重新启动Android设备或PC对用户来说不是一个实际的解决方案. :)

I faced the same issue and rebooting either the Android device or the PC is not a practical solution for users. :)

此问题是通过使用 MTP 协议(我讨厌此协议)引起的.你必须 启动对可用文件的重新扫描,您可以使用MediaScannerConnection类:

This issue is through the use of the MTP protocol (I hate this protocol). You have to initiate a rescan of the available files, and you can do this using the MediaScannerConnection class:

// Snippet taken from question
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirName");
path.mkdirs();

// Initiate a media scan and put the new things into the path array to
// make the scanner aware of the location and the files you want to see
MediaScannerConnection.scanFile(this, new String[] {path.toString()}, null, null);

这篇关于无法通过USB看到Android上添加的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆