2012-10-24 34 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/ –

相關問題