从图像文件路径填充Android Gallery?

从图像文件路径填充Android Gallery?,android,dynamic,path,image-gallery,Android,Dynamic,Path,Image Gallery,当应用程序启动时,我有一些图像被保存到Android设备上的一个目录中——我希望能够在图库中显示这些图像,但到目前为止我还无法做到这一点 我遵循示例库代码,但它使用可绘制的资源ID而不是文件路径。我发现这和我要找的很相似,只是它使用了ImageView而不是Gallery 因此,使用ImageView的代码如下所示: File imgFile = new File(“/data/data/com.myproject.example/files/someImage.png”); if(imgFi

当应用程序启动时,我有一些图像被保存到Android设备上的一个目录中——我希望能够在图库中显示这些图像,但到目前为止我还无法做到这一点

我遵循示例库代码,但它使用可绘制的资源ID而不是文件路径。我发现这和我要找的很相似,只是它使用了ImageView而不是Gallery

因此,使用ImageView的代码如下所示:

File imgFile = new  File(“/data/data/com.myproject.example/files/someImage.png”);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}
public ImageAdapter(Context c, int itemId) {
    context = c;

    imgArr = GlobalStore.getItem(itemId).getPhotos();
    TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);
    mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
    attr.recycle();
}
上面的代码可以工作,但我不知道如何使用图库。我一直在寻找答案,尝试不同的东西,但我对安卓系统的开发还不熟悉,我觉得自己有点不知所措


感谢您的帮助。先谢谢你。

基本上我认为你只需要把你的两个例子放在一起

首先使用Hello Gallery示例,但是您希望更改
ImageAdapter
getView()
方法中的代码,以调用
ImageView
上的
setImageBitmap()
,而不是
setImageResource()


您需要一个要加载的图像的文件路径数组/集合。

您需要做的事情如下:

File imgFile = new  File(“/data/data/com.myproject.example/files/someImage.png”);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}
public ImageAdapter(Context c, int itemId) {
    context = c;

    imgArr = GlobalStore.getItem(itemId).getPhotos();
    TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);
    mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
    attr.recycle();
}
正如你所看到的,这基本上是一个复制自画廊教程。在构造函数中,imgArr变量加载了一个JPG文件名数组。例如,这些是从数据库中读取的

然后在getView函数中有如下内容

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(context);


    String tmpStr = appContext.getFilesDir() + File.separator + "photos" + File.separator + imgArr.get(position);
    Bitmap bitmap = BitmapFactory.decodeFile(tmpStr);
    imageView.setImageBitmap(bitmap);
    imageView.setLayoutParams(new Gallery.LayoutParams(350, 300));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setBackgroundResource(mGalleryItemBackground);

    return imageView;
}
如您所见,getFilesDir()获取应用程序存储文件的数据位置,然后让我们假设所有照片都在“photos”目录中,您从imgArr数组中构建一个路径并附加一个文件名。因为这是为每张照片调用的,所以只需使用传递的位置变量

如果您没有照片数组,那么可能的方法是通过读取存储照片的目录来构建它,将所有文件名加载到一个数组中,然后执行此操作

然后在库侧执行其余操作,如库教程中所述