2011-07-15 10 views
1

私のアプリケーションでは、ユーザーが写真を撮って人々や証拠に関連付けるためのカメラを始めることができます。ルックアップテーブル(例:PersonMedia)にURIを保存し、関連付けられたピクチャのサムネイルをギャラリービューにロードします。ユーザはサムネイルから完全な画像を見ることができる。私が見つけたすべての例では、SDカードの全コンテンツからサムネイルをロードしていますが、特定のURIではサムネイルをロードできません。Android:データベースに保存されているURIの画像サムネイルギャラリー

私の2番目の選択肢は、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; 
    } 
関連する問題