gpt4 book ai didi

android - 显示来自 SD 卡或内部存储的图像以查看寻呼机 android

转载 作者:行者123 更新时间:2023-11-30 00:26:22 25 4
gpt4 key购买 nike

有一个 Activity ,它从可绘制文件夹中读取图像并在 viewpager 中显示它们,如幻灯片放映。下面是我的代码。现在我想将我的 SD 卡或内部存储中的图像显示到 viewpager 中。在下面的代码中应该做哪些更改才能从 SD 卡或内部存储中读取图像并将它们显示在 viewpager 中,如幻灯片放映。

Activity

    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);

}
}

最佳答案

您可以使用 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);

关于android - 显示来自 SD 卡或内部存储的图像以查看寻呼机 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45295581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com