2011-07-15 111 views
1

我的應用程序允許用戶啓動相機意圖拍攝圖片以與人員和證據關聯。我想將URI存儲在查找表(例如:PersonMedia)中,並將相關圖片的縮略圖加載到圖庫視圖中;隨後允許用戶從縮略圖查看完整圖像。我找到的每個示例都演示瞭如何從SD卡全部內容加載縮略圖,但不是從特定URI加載縮略圖。Android:從URI中存儲的圖像縮略圖庫存儲在數據庫中

我的第二個選擇是將字節數組存儲在SQLLite中,但我不是那麼喜歡的。任何幫助將不勝感激。

作爲一個方面說明,在過去,我無法弄清楚如何投票正確的答案。我終於明白了!是的,我有我的時刻...大聲笑。

回答

1

我解決了這個問題,通過提供一個動態目錄路徑直接從SD卡加載圖像。通過將URI存儲在數據庫中來回憶圖像並不像我預期的那樣工作。這是代碼,如果有人想做同樣的事情。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.personmedia); 

    personId = ((Global) this.getApplication()).getCurrentPersonID();  
    mediaCount = ((Global) getApplication()).getDataHelper().getPersonMediaCount(personId) + 1; 
    imageDirectory = Environment.getExternalStorageDirectory() + "/CaseManager/" + Long.toString(personId); 

    PopulateGallery(); 
} 

private void PopulateGallery() { 

    try { 
     if (LoadImageFiles() == true) { 
      GridView imgGallery = (GridView) findViewById(R.id.gallery); 

      imgGallery.setAdapter(new ImageAdapter(PersonMedia.this)); 

      // Set up a click listener 
      imgGallery.setOnItemClickListener(new OnItemClickListener() { 

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

         String imgPath = paths.get(position); 

         Intent intent = new Intent(getApplicationContext(), ViewImage.class); 
         intent.putExtra("filename", imgPath); 
         startActivity(intent); 
        } 
      }); 
     } 
    } catch (Exception ex) { 
     Log.e("PersonMedia.LoadPictures", ex.getMessage()); 
    } 

} 

私人布爾LoadImageFiles(){

try{ 
    mySDCardImages = new Vector<ImageView>(); 

    paths = new Hashtable<Integer, String>(); 

    fileCount = 0; 

    sdDir = new File(imageDirectory); 
    sdDir.mkdir(); 

    if (sdDir.listFiles() != null) 
    { 
     File[] sdDirFiles = sdDir.listFiles(); 

     if (sdDirFiles.length > 0) 
     {    
      for (File singleFile : sdDirFiles) 
      {     
       Bitmap bmap = decodeFile(singleFile); 
       BitmapDrawable pic = new BitmapDrawable(bmap); 

       ImageView myImageView = new ImageView(PersonMedia.this);      
       myImageView.setImageDrawable(pic); 
       myImageView.setId(mediaCount);  

       paths.put(fileCount, singleFile.getAbsolutePath()); 

       mySDCardImages.add(myImageView); 

       mediaCount++; 
       fileCount ++; 
      } 
     } 
     } 
    } 
     catch(Exception ex){ Log.e("LoadImageFiles", ex.getMessage()); } 

    return (fileCount > 0); 
} 

//decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f){  
    try {   
      //Decode image size   
      BitmapFactory.Options o = new BitmapFactory.Options();  
      o.inJustDecodeBounds = true;   
      BitmapFactory.decodeStream(new FileInputStream(f),null,o);  

      //The new size we want to scale to   
      final int REQUIRED_SIZE=100;   

      //Find the correct scale value. It should be the power of 2.   
      int width_tmp=o.outWidth, height_tmp=o.outHeight;   
      int scale=1;   

      while(true){    
       if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)     
        break;    
       width_tmp/=2;    
       height_tmp/=2;    
       scale*=2;   
      }   

      //Decode with inSampleSize   
      BitmapFactory.Options o2 = new BitmapFactory.Options();   
      o2.inSampleSize=scale;   

      return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);  
     } 

     catch (FileNotFoundException e) {}  

     return null; 
    }