gpt4 book ai didi

安卓 : Image thumbnail gallery from URI's stored in database

转载 作者:行者123 更新时间:2023-11-30 04:36:45 25 4
gpt4 key购买 nike

我的应用程序允许它的用户启动相机 Intent 来拍摄照片以与人和证据相关联。我想将 URI 存储在查找表中(例如:PersonMedia)并将相关图片的缩略图加载到图库 View 中;随后允许用户从缩略图查看完整图像。我找到的每个示例都演示了从 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());
}

}

私有(private) bool 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;
}

关于安卓 : Image thumbnail gallery from URI's stored in database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com