Android:来自SD卡的ViewPager Gallery

时间:2013-03-03 19:24:23

标签: android gallery sd-card

我有一些ViewPager图库的代码,但图像是从drawable文件夹中获取的,而我需要从SD卡上的文件夹中获取它们。

protected Integer[] mImageIds = {
        R.drawable.sample_1,
        R.drawable.sample_2,
        R.drawable.sample_3,
        R.drawable.sample_4,
        R.drawable.sample_5,
        R.drawable.sample_6,
        R.drawable.sample_7
};

这是需要更改的代码,如何使用sdcard中的图像替换drawable文件夹中的这些代码?

完整代码

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class Main extends FragmentActivity implements AdapterView.OnItemSelectedListener, ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private Gallery gallery;

    protected Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };
    private TextView textView;
    private Handler handler;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(this);

        gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter());
        gallery.setOnItemSelectedListener(this);

        textView = (TextView) findViewById(R.id.title);

        handler = new Handler();

    }

    // gallery item selected
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        updateUI(i);
    }

    // view pager item selected
    public void onPageSelected(int i) {
        updateUI(i);
    }

    private void updateUI(final int i) {
        handler.post(new Runnable() {
            public void run() {
                gallery.setSelection(i);
                viewPager.setCurrentItem(i);
                textView.setText("Photo #" + i);
            }
        });
    }

    public void onNothingSelected(AdapterView<?> adapterView) {    }

    public void onPageScrolled(int i, float v, int i1) {    }

    public void onPageScrollStateChanged(int i) {    }

    public class ImageAdapter extends BaseAdapter {

        public int getCount() {
            return mImageIds.length;
        }

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

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

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

            ImageView imageView = new ImageView(Main.this);

            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            return imageView;
        }
    }

    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public Fragment getItem(final int position) {
            return new Fragment() {
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    ImageView imageView = new ImageView(Main.this);
                    imageView.setImageResource(mImageIds[position]);
                    return imageView;
                }
            };
        }
    }


}

1 个答案:

答案 0 :(得分:0)

不是调用setImageResource,而是使用从SD卡加载的位图调用setImageDrawable。例如,从sdcard路径创建Bitmap,然后setImageDrawable

Bitmap bitmap = BitmapFactory.decodeFile(path_of_file);
imageView.setImageDrawable(bitmap);

注意,不应该对UI线程进行图像解码。 android文档讨论efficiently loading images off the ui thread。所讨论的AsyncTask可以通过一行或两行更改来修改以满足您的需求。