无法从 android 模拟器 (Titanium) 获取文件 [英] Having trouble to get the files from android emulator (Titanium)

查看:23
本文介绍了无法从 android 模拟器 (Titanium) 获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们!我无法从我的 android 模拟器中获取文件!

guys! I have trouble to get files from my android emulator!

①这是我的模拟器信息:Genymotion 供个人使用 - 三星 Galaxy Note 3 - 4.4.4 - API 19 - 1080x1920

① Here is my emulator info: Genymotion for personal use - Samsung Galaxy Note 3 - 4.4.4 - API 19 - 1080x1920

②这里是模拟器的文件树:

② And here is the file's tree of the emulator:

我在Internal storage目录下存储了两个mp3文件供测试,路径是/storage/emulated/0/Download.

I had stored two mp3 files for test in the Internal storage directory, and the path is /storage/emulated/0/Download.

如您所见,这两个 mp3 文件位于 Download 目录中.我的问题是我永远无法将这两个文件放在正确的路径上.

As you can see, the two mp3 files are in the Download directory. My trouble is that I can never get the two files right path.

③ 这是我的代码:

if(Ti.Filesystem.isExternalStoragePresent()){
    var sdkDir_1 = Ti.Filesystem.getExternalStorageDirectory();
    console.info('------------->sdkDir_1');
    console.info(sdkDir_1);

    var sdkDir_2 = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory);
    console.info('------------->sdkDir_2');
    console.info(sdkDir_2);

    var downLoadDir = Ti.Filesystem.getFile(sdkDir_2.parent.directoryListing[7]);
    console.info('------------->downLoadDir.getDirectoryListing()');
    console.info(downLoadDir.getDirectoryListing());
    ...

这是控制台:

[INFO] [android, 4.4.4, 10.0.3.15] ------------->sdkDir_1
[INFO] [android, 4.4.4, 10.0.3.15] appdata://

[INFO] [android, 4.4.4, 10.0.3.15] ------------->sdkDir_2
[INFO] [android, 4.4.4, 10.0.3.15] {
"readonly": false,
"bubbleParent": true,
"writable": true,
"directoryListing": [],
"symbolicLink": false,
"apiName": "Ti.Filesystem.File",
"hidden": false,
"nativePath": "file:///storage/emulated/0/co.test.zone",
"executable": false,
"size": 4096,
"name": "co.happysoft.zone",
"parent": {
    "readonly": false,
    "bubbleParent": true,
    "writable": true,
    "directoryListing": [
        "Music",
        "Podcasts",
        "Ringtones",
        "Alarms",
        "Notifications",
        "Pictures",
        "Movies",
        "Download",
        "DCIM",
        "Android",
        "storage",
        ...

 [INFO] [android, 4.4.4, 10.0.3.15] ------------->downLoadDir
 [INFO] [android, 4.4.4, 10.0.3.15] {
        "readonly": true,
        "bubbleParent": true,
        "writable": false,
        "directoryListing": [],
        "symbolicLink": false,
        "apiName": "Ti.Filesystem.File",
        "hidden": false,
        "nativePath": "file:///android_asset/Resources/api/Download",
        "executable": false,
        "size": 0,
        "name": "/Download",
        "parent": null
  }

我很困惑,Download 路径应该如何改变,而它的 directoryListing 什么都没有.而且,我想知道为什么 sdkDir_1 也没有.谁有好的解决方案,提前谢谢.

I'm so confused that how the Download path should changed and it's directoryListing has nothing. And, I'm wonder why the sdkDir_1 has nothing too. Anyone have a good solution to me, thanks in advance.

推荐答案

好吧,非常感谢 @Fokke Zandbergen 提醒我我犯了一个错误,试图通过外部存储目录.现在,很明显,如果您想从模拟器或真正的移动设备获取本地文件,也许以下建议可能对您有所帮助:

Well, thanks @Fokke Zandbergen so much that remind me I had made a mistake that try to get the emulator's internal files through the external storage directory. And now, it's clear that if you want to get the local files from the emulator or real mobile device, maybe the follow suggestions may help you:

①从模拟器中获取本地文件

1 Android,试试此路径:file:///storage/emulated/0

首先,emulated目录是模拟器专用的,所有安卓模拟器都有这个目录.然后,目录0表示你的模拟器只有一个内部存储目录,因为模拟器没有外部存储sdcard.

First, the directory emulated is only special for the emulator, all the android emulator have this directory. And then, the directory 0 is means your emulator only have one internal storage directory, cause there're no external storage sdcard for the emulator.

[2] iPhone,试试这个路径:/Users/xxx/Library/Developer/CoreSimulator/Devices/F34851D0-AFA5-443E-AEF6-70B695FB9C5E/data

[2] iPhone, try this path: /Users/xxx/Library/Developer/CoreSimulator/Devices/F34851D0-AFA5-443E-AEF6-70B695FB9C5E/data

F34851D0-AFA5-443E-AEF6-70B695FB9C5E目录是模拟器ID,可以通过ti info命令查看.

For the directory F34851D0-AFA5-443E-AEF6-70B695FB9C5E, it's the simulator ID which you can check it by ti info command.

②从真实移动设备获取本地文件

1 Android,试试这个路径:file:///storage/sdcard0

请注意,sdcard0 是您移动设备的内部存储.如果您需要从外部存储目录获取本地文件,也意味着SD卡,那么您应该尝试这种方式:

Note this, sdcard0 is the internal storage of your mobile device. If you need to get the local files from external storage directory, also means SD card then you should try this way:

if(Ti.Filesystem.isExternalStoragePresent()){
    var sdkDir = Ti.Filesystem.getExternalStorageDirectory();
}

[2] iPhone,因为iPhone没有外置存储,所以和模拟器一样.嗯,我没有测试过这种方式,也许你应该尝试一下.

[2] iPhone, cause iPhone have not the external storage, so its same to the simulator. Well, I have not test this way, maybe you should have a try.

各位,我最近测试了真正的移动设备文件路径,我指的是SDCard(external storage)的文件路径.但是,我要告诉你一个坏消息,Titanium API 负担得起检测外部存储是否存在不好使用.详情如下:

Well guys, I had tested the real mobile device file path recently, which I mean the file path of SDCard(external storage). However,there's a bad news I should tell you, the method that Titanium API afford to detect the if the external storage exists is not good to use. Here is the details:

  1. if(Ti.Filesystem.isExternalStoragePresent()) 总是返回 true,尽管 SDCard 不存在.

我想,如今,几乎所有的智能手机都内置了内部存储(绝不意味着智能手机的自身存储就像电话簿,我的意思是内部存储是一个特殊的外部存储,但嵌入到智能手机中),SDCard是一个扩展存储,但它们是相同的.

I supposed that, nowadays, almost all of the smartphones were built embed with a internal storage (never means the smartphone's selves storage like phonebook, I mean the internal storage is a special external storage but embed into the smartphone), the SDCard is a extend storage, but they're same.

不同厂家的外置存储设置不同

在我的测试中,当智能手机来自华为时,外部存储路径变为:file:///storage/sdcard1,区分带内部存储sdcard0.

During my test, when the smartphone comes from HUAWEI the external storage path become: file:///storage/sdcard1, distinguished with internal storage sdcard0.

但是,当制造商为SAMSUNG时,外部存储路径为:file:///storage/extSdCard.

However, when the manufacturer is SAMSUNG, the external storage path is: file:///storage/extSdCard.

所以,我们必须检查制造商来定义外部存储路径:

So, we have to check the manufacture to define the external storage path:

var sdCardPath;
if(Ti.Platform.manufacturer == 'HUAWEI'){
    sdCardPath = Ti.Filesystem.getFile('file:///storage/sdcard1');
}
else if(Ti.Platform.manufacturer == 'samsung'){
    sdCardPath = Ti.Filesystem.getFile('file:///storage/extSdCard');
}

嗯,太可怕了!

所以,我一直没有一个好主意来处理来自不同制造商的真实移动设备的文件.小伙伴们,你们有什么好主意吗?

So, I have been not get a good idea to approach the file from the real mobile devices by different manufacture. Guys, do you have a good idea?

这篇关于无法从 android 模拟器 (Titanium) 获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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