使用 phonegap 调用本机文件浏览器 [英] Invoke native file browser using phonegap

查看:21
本文介绍了使用 phonegap 调用本机文件浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的 phonegap 项目中实现文件上传功能.用户应该能够从手机内存或 SD 卡上传任何类型的文件.我使用 jQuery Mobile 框架设计的应用程序屏幕.我试过 input type="file",但它在 android 4.4 中不受支持.我也尝试过 phonegap 相机 API,但它只支持媒体文件.我找到了一些cordova插件(exm1,exm2).但是这些插件使用自定义 UI.我想调用本机文件浏览器来选择文件 &它必须在 Android 和iPhone 平台.有没有办法实现相同的?

I have to implement file upload feature in my phonegap project. User should be able to upload any type of file from the phone memory or sd card. The application screens I designed using jQuery Mobile framework. I tried input type="file", but it is not supported in android 4.4. I tried phonegap camera API too, but it is supported only media files. I found some cordova plugins (exm1,exm2 ). But these plugins using custom UI. I want to invoke native file browser for choosing the file & it has to work in both Android & iPhone platforms. Is there a way to implement the same?

我发现cordova文件选择器插件(https://github.com/cdibened/filechooser)会对android平台有帮助,但我无法让它工作.选择文件后不会立即触发成功回调函数(使用 android 4.4.2 测试).请在下面找到我的代码,

I found cordova file chooser plugin (https://github.com/cdibened/filechooser) would be helpful for android platform, but I am unable to make it work. The success callback function is not immediately getting triggered after the file selection (tested with android 4.4.2). Please find my code below,

$("#fileinput").bind('click',function(){ 
    console.log("choose file selected"); 
    filechooser.open( {}, fileChooseSuccess, fileChooseFailed ); 
}); 
function fileChooseSuccess(data) { 
    var filepath = data.filepath; 
    console.log("file path:"+filepath); 
} 
function fileChooseFailed(msg) { 
    console.log(msg); 
} 

推荐答案

我能让你的插件 FileChooser 工作.

I was able to get your plugin, FileChooser working.

虽然有些事情必须做.你需要用你的编辑器打开以下内容

There are certain things that have to be done though. You need to open the following with your editor

  • 文件选择器.java
  • FileChooserActivity.java
  • FileListAdapter.java
  • FileListFragment.java
  • LocalStorageProvider.java

  • FileChooser.java
  • FileChooserActivity.java
  • FileListAdapter.java
  • FileListFragment.java
  • LocalStorageProvider.java

并附加

import your.package.name.R;

到每个文件.

这是我使用的演示代码:

Here is the demo code I used:

<html>
<head>
    <title>Hello World</title>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script type="text/javascript">
    document.addEventListener("deviceready", function(){

        var success = function(data) {
            alert("File chosen: " + data.filepath); 
        };

        var error = function(msg) {
            console.log( msg );
        };

        $('#fileinput').click(function(e) {
            filechooser.open({}, success,error);
        });
     });
    </script>

</head>

<body>
     <input type="file" id="fileinput" name="fileinput"/>
</body>
</html>

另外,请注意作者打算在 KitKat 4.4.4 中使用它.它可能适用于较低版本,但他不确定.

Also, be aware that the author intended this to be used in KitKat 4.4.4. It may work with lower versions but he's uncertain.

请注意,HTML5 选择窗口与此窗口之间的唯一区别是内部存储"选项.

Take note that the only difference between the HTML5 choice window and this is the "Internal Storage" option.

希望这会有所帮助.

这篇关于使用 phonegap 调用本机文件浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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