我的应用程序允许用户启动相机意图拍照以与人和证据相关联。我想将URI存储在查找表中(例如:PersonMedia),并将相关图片的缩略图加载到图库视图中;随后允许用户从缩略图中查看完整图像。我找到的每个例子都演示了从SD卡的全部内容加载缩略图,而不是从特定的URI加载缩略图。
我的第二个选择是将字节数组存储在SQLLite中,但我不是那个的忠实粉丝。任何帮助将不胜感激。
作为旁注,过去我无法弄清楚如何对正确答案进行投票。我终于明白了!是的,我有我的时刻......哈哈。
答案 0 :(得分: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());
}
}
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;
}