将图像从文件夹路径加载到网格视图,Android

时间:2014-02-27 10:15:01

标签: android gridview directory

您好我遇到了一点问题。我找不到一个允许我加载所有内容的工作示例 从一个文件夹到网格视图的图像。 我有一个工作的网格视图,但我必须通过带有R.drawable.theImageName的int数组加载图像 我需要的是一个功能,可以将所有图像加载到网格,而无需知道图像的名称。 我已按照本教程创建网格视图http://www.youtube.com/watch?v=Y75i3U7X-ng

Tnx的帮助 如果我忘了一些东西,我对应用程序编程操作系统有点新,请告诉我,我会尝试粘贴代码。

GridView gv = (GridView) findViewById(R.id.car_gridView);
    gv.setAdapter(new CarImageAdapter(getApplicationContext()));

private Context context;
public CarImageAdapter(Context applicationContext) {
        // TODO Auto-generated constructor stub
        context = applicationContext;
    }

    public View getView(int position, View contentView, ViewGroup parrent) {
    // TODO Auto-generated method stub
    ImageView iv;
    if(contentView != null)
    {
        iv = (ImageView) contentView;
    }
    else
    {
        iv = new ImageView(context);
        iv.setLayoutParams(new GridView.LayoutParams(300, 300));
        iv.setScaleType(ScaleType.CENTER_CROP);
        iv.setPadding(8, 8, 8, 8);

    }

    iv.setImageResource(car_images[position]);  
    return iv;
}

我用它来创建和保存图像。

    public void captureImage()
{
    ImageView imageView = (ImageView) findViewById(R.id.img_resultimage);

    root = Environment.getExternalStorageDirectory().toString() + "/Upgrade_rim";
    imageFolderPath = root + "/car_images";
    File imagesFolder = new File(imageFolderPath);
    imagesFolder.mkdirs();


    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    imageName = "car_"+timeStamp +".png";


    File image = new File(imageFolderPath, imageName);
    fileUri = Uri.fromFile(image);
    imageView.setTag(imagesFolder + File.separator + imageName);
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(takePictureIntent, CAMERA_IMAGE_REQUEST);

}

2 个答案:

答案 0 :(得分:0)

我列出了存储在sdcard根文件夹中的所有png文件。我用PDF文件成功完成了这项工作。

public class Sdcard_pdf_list extends ListActivity {

    String[] pdflist;
    File[] imagelist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    File images = Environment.getExternalStorageDirectory();
        imagelist = images.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) 
            {
                return ((name.endsWith(".png")));
            }

        });

        pdflist = new String[imagelist.length];
        for (int i = 0; i < imagelist.length; i++) {

            pdflist[i] = imagelist[i].getName();

        }
        this.setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, pdflist));

    }


}

如果您有任何疑问,请填写免费通知。

答案 1 :(得分:0)

  

一种选择是使用Assets文件夹。

将所有图片放在一个文件夹中,在资产文件夹中说“产品”。

我使用ImageView来显示图像。您可以相应地将其更改为gridview。

你走了:

public class Assets_Folder_Img extends Activity 
{

    ArrayList<String> arr_Img = new ArrayList<String>();
    String ASSET_FOLDER_NAME="products";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assets__folder__img);

        get_images_in_List(ASSET_FOLDER_NAME);
        setImages();
    }

    void setImages()
    {

        InputStream is = null;

        for(int i=0;i<arr_Img.size();i++)
        {
            try 
            {
                is = getAssets().open(ASSET_FOLDER_NAME+"/" +arr_Img.get(i));
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Bitmap bit = BitmapFactory.decodeStream(is);

            LinearLayout ll = (LinearLayout)findViewById(R.id.root);

                ImageView image = new ImageView(this);
                image.setLayoutParams(new android.view.ViewGroup.LayoutParams(120,120));
                image.setImageBitmap(bit);
                ll.addView(image);

        }
    }

     private void get_images_in_List(String folderName) 
        {
            try 
            {
                String [] list = getAssets().list(folderName);

                    arr_Img.addAll(Arrays.asList(list));
                    Log.e("log", "size is "+arr_Img.size());
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

        }
}

将图像放入资源仅适用于少量图像存储。否则会给应用程序带来负担。