从SD卡或内部存储器显示图像以查看寻呼机android

时间:2017-07-25 06:34:41

标签: android android-layout android-asynctask android-viewpager

有一个活动,它从drawable文件夹中读取图像并在viewpager中显示它们,如幻灯片放映。下面是我的代码。现在我想将我的SD卡或内部存储中的图像显示到viewpager中。应在下面的代码中进行哪些更改,以便从SD卡或内部存储中读取图像,并在幻灯片放映等viewpager中显示。

活动

    public class MainActivity extends AppCompatActivity {
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewPager);

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);

        viewPager.setAdapter(viewPagerAdapter);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTimerTask(), 3000,3000);

    }
    public class MyTimerTask extends TimerTask {

        @Override
        public void run() {

            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    if (viewPager.getCurrentItem() == 0) {
                        viewPager.setCurrentItem(1);
                    } else if (viewPager.getCurrentItem() == 1) {
                        viewPager.setCurrentItem(2);
                    } else if (viewPager.getCurrentItem() == 2) {
                        viewPager.setCurrentItem(3);
                    } else if (viewPager.getCurrentItem() == 3) {
                        viewPager.setCurrentItem(4);
                    } else {
                            viewPager.setCurrentItem(0);
                        }

                    }


                ;

            });
        }
    }
}

ViewPagerAdapter

    public class ViewPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
   private Integer [] images = {R.drawable.index1,R.drawable.index2,R.drawable.index3,R.drawable.index4,R.drawable.index5};

    public ViewPagerAdapter(Context context) {

        this.context = context;
    }
    @Override
    public int getCount() {

        return images.length;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view == object;
    }
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.custom_layout, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
        imageView.setImageResource(images[position]);


        ViewPager vp = (ViewPager) container;
        vp.addView(view, 0);
        return view;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        ViewPager vp = (ViewPager) container;
        View view = (View) object;
        vp.removeView(view);

    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Picasso从SD卡加载图像。指南:
Picasso Library

在你的情况下:

    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); // retrieve the storage dir
    Picasso.with(context).load(new File(baseDir + File.separator + your_filename)).into(imageView);