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

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

附带说明一下,过去我无法弄清楚如何对正确答案进行投票。我终于明白了!是的,我有我的时刻...大声笑。

最佳答案

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

07-24 09:48
查看更多