Android:来自存储在数据库中的 URI 的图像缩略图库

发布于 2024-11-25 01:18:36 字数 259 浏览 3 评论 0原文

我的应用程序允许用户启动相机意图拍照以与人和证据相关联。我想将 URI 存储在查找表中(例如:PersonMedia)并将相关图片的缩略图加载到图库视图中;随后允许用户从缩略图中查看完整图像。我找到的每个示例都演示了从 SD 卡完整内容加载缩略图,而不是从特定 URI 加载缩略图。

我的第二个选择是将字节数组存储在 SQLLite 中,但我不太喜欢这样做。任何帮助将不胜感激。

顺便说一句,过去我不知道如何对正确答案进行投票。我终于明白了!是的,我也有我的时刻……哈哈。

My application allows it's user to start a camera intent to take pictures to associate with people and evidence. I want to store the URI in a lookup table (ex: PersonMedia) and load thumbnails of the associated pictures into a gallery view; subsequently allowing the user to view the full image from the thumbnail. Every example I find demonstrates loading thumbnails from the SD Cards full contents but not from specific URI's.

My second choice being to store the byte array in SQLLite, but I'm not a big fan of that. Any help would be greatly appreciated.

As a side note, in the past I couldn't figure out how to vote on the correct answer. I finally got it! Yeah, I have my moments...lol.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2024-12-02 01:18:36

我通过提供动态目录路径直接从 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;
  }

I solved this by loading images directly from the sd card by providing a dynamic directory path. Recalling the images by storing the URI's in the database didn't work the way I expected it to. Here is the code if anyone wants to do the same type thing.

@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());
    }

}

private boolean 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;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文