1

我开发了一个应用程序,想要一个打开图片库的链接。图像在本地捆绑并包含在我的 assets/www/img 目录中。我希望它能够无缝地类似于原生图片库。这可能吗?

我尝试使用以下内容,但无法得到回复。

document.addEventListener("deviceready", onDeviceReady, false);

        // PhoneGap is ready
        //
        function onDeviceReady() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
        }

        function onFileSystemSuccess(fileSystem) {
            fileSystem.root.getDirectory("img/china", {create: false, exclusive: false}, getDirSuccess, fail);
        }

        function getDirSuccess(dirEntry) {
            // Get a directory reader
            var directoryReader = dirEntry.createReader();

            // Get a list of all the entries in the directory
            directoryReader.readEntries(readerSuccess,fail);
        }

        var numDirs = 0;
        var numFiles = 0;

        var readerTimeout = null, millisecondsBetweenReadSuccess = 100;

        function readerSuccess(entries) {
            var i = 0, len = entries.length;
            for (; i < len; i++) {
                if (entries[i].isFile) {
                    numFiles++;
                    entries[i].file(fileSuccess,fail);
                } else if (entries[i].isDirectory) {
                    numDirs++;
                    getDirSuccess(entries[i]);
                }
                if (readerTimeout) {
                    window.clearTimeout(readerTimeout);
                }
            }
            if (readerTimeout) {
                window.clearTimeout(readerTimeout);
            }
            readerTimeout = window.setTimeout(weAreDone, millisecondsBetweenReadSuccess);
        }

        // additional event to call when totally done
        function weAreDone() {
           // do something
        }
4

2 回答 2

0

问题是您正在尝试使用 File API 来读取应用程序资产目录中的文件。该目录实际上不是文件系统的一部分。当您执行 window.requestFileSystem(LocalFileSystem.PERSISTENT, ...) 时,您实际上正在获取 /sdcard (很可能)。因此,当您尝试加载“img/china”时,该目录不存在。

您需要在主 Java 类的 onCreate 方法期间将文件从资产目录复制到本地文件系统。然后您可以使用 DirectoryReader 获取所有文件的列表。

或者,如果您提前知道所有图像,您可以创建自己的 JSON 文件来列出所有图像。然后加载该文件并能够遍历所有条目以创建您自己的画廊。

于 2012-10-25T13:40:01.553 回答
0

相机文档涵盖了这一点。这不适合你吗?

如果 Camera.sourceType = Camera.PictureSourceType.PHOTOLIBRARY 或 Camera.PictureSourceType.SAVEDPHOTOALBUM,则会显示照片选择器对话框,从中可以选择相册中的照片。

http://docs.phonegap.com/phonegap_camera_camera.md.html

也可以参考...

使用 PhoneGap 和 Android 浏览图片库

我正在将我为 iphone 编写的应用程序移植到 Android 上,我想从相机功能开始。我环顾四周并用谷歌搜索,但找不到有关如何在 Android 设备上浏览图片库的文档。我决定暂时对现有的 Camera 对象进行一些简单的修改以支持该功能。我的未来,希望 PhoneGap 的人能更好地实现这一点,我可以把这段代码放到牧场上。

此代码需要添加到 phoneGap.js 文件中。它为在设备上浏览画廊的新方法创建原型

1   //
2   // add the new prototype to support the browse function
3   //
4   // i have created a new class 'PixCameraLauncher' so I did not have to modify
5   // the original class file provided by PhoneGap
6   //
7   com.phonegap.CameraLauncherProxy.prototype.browseImages = function(quality) {
8       return PhoneGap.exec("com.phonegap.gapTest.PixCameraLauncher", "browseImages", [quality]);
9   };

这是我对 PhoneGap 提供的 CameraLauncher 类所做的修改。添加新意图以启动图片库

01  /**
02   * Get a picture from the Image Gallery.
03   *
04   * in DroidGap.onActivityResult, which forwards the result to
05   * this.onActivityResult.
06   *
07   * @param quality
08   *            Compression quality hint (0-100: 0=low quality & high
09   *            compression, 100=compress of max quality)
10   */
11  public void browseImages(int quality) {
12      this.mQuality = quality;
13   
14      // Display camera
15      Intent intent = new Intent(Intent.ACTION_PICK,
16              android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
17   
18      this.ctx.startActivityForResult((Plugin) this, intent);
19  }

我们还需要处理返回的文件。我把它简单化了,但这可以稍微清理一下我所做的是假设如果数据字段中有一个 URI,那么一个图像已经被挑选出来并且应该由实用程序处理。在开始时

1   void onActivityResult(int requestCode, int resultCode, Intent intent)

添加下面的代码。这将处理将 URI 重新分配给所选文件

1   // If image available
2   if (resultCode == Activity.RESULT_OK) {
3    
4       //
5       // this means the user selected an image, so just pass it back!!
6       //
7       if (intent.getData() != null) {
8           imageUri = intent.getData();
9       }

经过更多的测试和代码清理后,我将为 android 工作和 iphone 工作提供完整的项目。如果有任何问题,请在下方留言。

于 2012-10-25T05:35:45.573 回答