2012-10-24 36 views
1

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

我试图使用以下,但不能得到一个回应。

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 
     } 

回答

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上运行。如果有任何问题,请在下面张贴。

0

问题是您正尝试使用File API来读取应用程序资产目录中的文件。该目录实际上不是文件系统的一部分。当你做一个window.requestFileSystem(LocalFileSystem.PERSISTENT,...)时,你实际上得到/ sdcard(最可能)。所以当你试图加载“img/china”那个目录不存在的时候。

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

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

+0

谢谢你的详细答案,我确实知道我的所有图片,所以探索JSON选项可能是我最好的选择。你有关于如何从JSON文件创建我自己的画廊的任何文档? –

+0

你可能不得不扮演你自己的角色。查看:http://speckyboy.com/2009/06/03/15-amazing-jquery-image-galleryslideshow-plugins-and-tutorials/ –

相关问题