Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android,直接打开mtp文件夹_Java_Android_Android Intent_Mtp - Fatal编程技术网

Java Android,直接打开mtp文件夹

Java Android,直接打开mtp文件夹,java,android,android-intent,mtp,Java,Android,Android Intent,Mtp,我正在做一个项目,需要通过USB电缆将智能手机连接到平板电脑。为了从平板电脑上访问智能手机上的文件,我使用Android的Intent.ACTION\u GET\u CONTENT检索文件。更确切地说,我是在尝试打开图片 我知道这是可能的,因为当我点击描述智能手机的通知时,它会打开一个默认的Android应用程序,其中包含智能手机的基本文件夹 我试图实现的是直接打开连接的智能手机的存储,而不必从平板电脑切换到智能手机的存储 我知道如何获取智能手机的卷名,但这并没有帮助我取得进步。我已经检查了从I

我正在做一个项目,需要通过USB电缆将智能手机连接到平板电脑。为了从平板电脑上访问智能手机上的文件,我使用Android的
Intent.ACTION\u GET\u CONTENT
检索文件。更确切地说,我是在尝试打开图片

我知道这是可能的,因为当我点击描述智能手机的通知时,它会打开一个默认的Android应用程序,其中包含智能手机的基本文件夹

我试图实现的是直接打开连接的智能手机的存储,而不必从平板电脑切换到智能手机的存储

我知道如何获取智能手机的卷名,但这并没有帮助我取得进步。我已经检查了从Intent收到的Uri,它类似于
content://com.android.mtp.documents/document/951
content://com.android.mtp.documents/tree/1021

我曾尝试使用该Uri打开所需的文件夹,但没有成功。 我尝试了新的
StorageManager
API,但没有成功

最近,我更专注于尝试从我的应用程序简单地访问文件,但使用上面提到的Uri或mount文件夹(
/dev/bus/usb/001/002/
)将一事无成。 我还尝试了
环境。媒体安装
,但没有成功

我当前的代码是: 正在请求使用智能手机的权限:

final UsbManager manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
if (deviceList.size() > 0) {//TODO switch to 1 on USBCameraFragment
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
    UsbDevice device = (UsbDevice) deviceList.values().toArray()[0];
    manager.requestPermission(device, mPermissionIntent);
} else {
    Toast.makeText(getContext(), "Please connect your device to the tablet.", Toast.LENGTH_SHORT).show();
}

您是否尝试过Intent.ACTION\u OPEN\u DOCUMENT\u TREE?
已尝试使用该Uri打开所需文件夹
。这些是文件的URI。你说的是哪个文件夹?您尝试了什么?
uripath=MediaStore.Files……
path.toString()的值是多少?
i.setDataAndType(path,“image/*”)设置数据没有效果。你见过效果吗?只有类型有影响。我尝试了Intent.ACTION\u OPEN\u DOCUMENT\u树,它返回了以下Uri:
content://com.android.mtp.documents/tree/1021
(智能手机的基本文件夹)。我试过
content://com.android.mtp.documents/tree/1021
打开基本文件夹并
content://com.android.mtp.documents/document/951
要打开一个文件,两者都不起作用。MediaStore提供
content://media/SAMSUNG_Android/files
。事实上,设置数据并没有改变目录(我用平板电脑上的一个目录进行了检查)。
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if(device != null){
                        Intent i = new Intent(Intent.ACTION_GET_CONTENT);

                        Uri path = MediaStore.Files.getContentUri(device.getProductName());
                        //Uri path = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.MEDIA_MOUNTED).getAbsolutePath());

                        i.setDataAndType(path, "image/*");
                        i.addCategory(Intent.CATEGORY_OPENABLE);

                        MtpDevice mtpDevice = new MtpDevice(device);

                        UsbDeviceConnection conn = ((UsbManager) getActivity().getSystemService(Context.USB_SERVICE)).openDevice(device);
                        mtpDevice.open(conn);

                        //1.5s to make sure I have allowed the MTP connection on the smartphone.
                        try{Thread.sleep(1500);}catch(InterruptedException e){}

                        startActivityForResult(i, Constants.PICKFILE_REQUEST_CODE);
                    }
                }
                else {
                    Log.d(TAG, "permission denied for device: " + device);
                }
            }
        }
    }
};