0

我有一个活动,我在其中显示媒体文件列表,即视频、音频、图像和动画。单击列表项时(截至目前的图像),活动必须在网格视图中显示本地资产文件夹中的所有图像。为此,我使用单个适配器并在我的 getView() 函数中有一个开关盒。根据构造函数中设置的选项,将执行切换案例。它适用于 ListView 显示,但我无法在网格视图中显示图像列表。任何帮助将不胜感激。提前致谢。这是我的代码:

    package com.bookshelf;
    import java.io.File;
    import java.io.FilenameFilter;
    import java.io.IOException;
    import java.util.ArrayList;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.content.res.AssetManager;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.media.ThumbnailUtils;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    public class MediaGalaryListActivity extends Activity implements
    OnItemClickListener {

private ArrayList<String> mGalary = new ArrayList<String>();
private Bitmap mBitArray[];
private Gallery mMediaGallery;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mediagalary);
    mGalary.add("Videos");
    mGalary.add("Audios");
    mGalary.add("Images");
    mGalary.add("Animation");
    ListView lv = (ListView) findViewById(R.id.mediaGal);
    mMediaGallery = (Gallery) findViewById(R.id.mediaGallery);

    lv.setAdapter(new MediaGalaryAdapter(this, mGalary, 1));
    lv.setOnItemClickListener(this);
}

class MediaGalaryAdapter extends BaseAdapter {

    private ArrayList<String> mGal = new ArrayList<String>();
    private Bitmap[] mImgArray;
    private Context context;
    private LayoutInflater mInflate;
    private int mAdapterOpt;

    public MediaGalaryAdapter(Context ctx, ArrayList<String> gal,
            int adapOpt) {
        context = ctx;
        mGal = gal;
        mAdapterOpt = adapOpt;
        mInflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public MediaGalaryAdapter(Context ctx, Bitmap[] imgArray, int adapOpt) {
        context = ctx;
        mImgArray = imgArray;
        mInflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mAdapterOpt = adapOpt;
    }

    public int getCount() {
        int size = 0;
        switch (mAdapterOpt) {
        case 1:
            size = mGal.size();
            break;
        case 2:
            size = mImgArray.length;
            break;
        }
        return size;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        switch (mAdapterOpt) {
        case 1:
            convertView = mInflate.inflate(R.layout.medialayout, null);
            TextView tv = (TextView) convertView.findViewById(R.id.text);

            tv.setText(mGal.get(position));
            break;
        case 2:
            ImageView imgView;

            convertView = mInflate.inflate(R.layout.image_gallery, null);

            imgView = new ImageView(context);
            imgView.setImageBitmap(mImgArray[position]);
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            imgView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imgView.setPadding(8, 8, 8, 8);


            break;
        }

        return convertView;
    }
}

// For filtering the filename with extensions
class FileNamFilter implements FilenameFilter {
    private String mFileExtn;

    public FileNamFilter(String extn) {
        mFileExtn = "." + extn;
    }

    public boolean accept(File dir, String filename) {
        return filename.endsWith(mFileExtn);
    }
}

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
    String mediaType = mGalary.get(pos);


    String list[] = null;
    AssetManager assetManager = getAssets();
    try {
        list = assetManager.list("Immersive");
        mBitArray = new Bitmap[list.length];

        System.out.println("Length of list ="+list.length);


        for (int i = 0, idx = 0; i < list.length; i++) 
        {

            if (list[i].endsWith(".png") || list[i].endsWith(".gif")
                    || list[i].endsWith(".jpeg")
                    || list[i].endsWith(".jpg"))
            {
                mBitArray[idx++] = BitmapFactory.decodeStream(assetManager
                        .open("Immersive/" + list[i]));
                System.out.println("Image at position "+i+" is "+list[i]);
            }
            ;

        }
        mMediaGallery
                .setAdapter(new MediaGalaryAdapter(this, mBitArray, 2));
    } catch (IOException e) {
        e.printStackTrace();
    }



    AlertDialog.Builder build = new AlertDialog.Builder(this);
    build.setTitle("InProgress....");
    // build.setIcon(android.R.drawable.)
    build.setMessage(mediaType + " is Inprogress...");
    build.setPositiveButton("Ok", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // finish();
        }
    });
    AlertDialog alert = build.create();
    alert.show();
    // Toast.makeText(getApplicationContext(), mediaType, 30).show();
}

class MediaGalary {
    private ImageView mImage;
    private TextView mName;

    public MediaGalary(ImageView img, TextView strName) {
        mImage = img;
        mName = strName;
    }

    public ImageView getmImage() {
        return mImage;
    }

    public void setmImage(ImageView mImage) {
        this.mImage = mImage;
    }

    public TextView getmName() {
        return mName;
    }

    public void setmName(TextView mName) {
        this.mName = mName;
    }
}

}

4

1 回答 1

1

设备中的每个应用程序都将由 DVM 分配一定数量的内存。您正在出现内存不足错误,因为您的应用程序超出了 dvm 分配给您的应用程序的内存。例如在 froyo 中,为应用程序分配的内存为 16Mb。如果您的应用程序超过 16Mb,则会出现内存不足错误。解决方案是您必须压缩您在应用程序中使用的图像,并且您必须清除所有正在使用的集合。完成工作后,尝试清除您正在使用的所有集合。您可以使用堆工具在 ddms 中检查您的应用程序消耗了多少内存。希望这对您有所帮助。

于 2012-12-21T05:13:14.240 回答