Đoạn mã sau truy xuất nội dung của thư viện và đặt mọi đường dẫn hình ảnh bên trong danh sách mảng. Sau đó, chọn ngẫu nhiên một trong các đường dẫn bên trong ArrayList và đặt làm tài nguyên cho một ImageView
Handler handler = new Handler();
protected int counter = 0;
private ImageView mImageView;
private Bitmap currentBitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
mImageView = (ImageView) findViewById(R.id.imageView);
String[] projection = new String[]{
MediaStore.Images.Media.DATA,
};
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = managedQuery(images,
projection,
"",
null,
""
);
final ArrayList<String> imagesPath = new ArrayList<String>();
if (cur.moveToFirst()) {
int dataColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATA);
do {
imagesPath.add(cur.getString(dataColumn));
} while (cur.moveToNext());
}
cur.close();
final Random random = new Random();
final int count = imagesPath.size();
handler.post(new Runnable() {
@Override
public void run() {
int number = random.nextInt(count);
String path = imagesPath.get(number);
if (currentBitmap != null)
currentBitmap.recycle();
currentBitmap = BitmapFactory.decodeFile(path);
mImageView.setImageBitmap(currentBitmap);
handler.postDelayed(this, 1000);
}
});
}
Hãy chắc chắn trao giải thưởng đúng thời hạn cho các phản hồi của họ như Blackbelt bên dưới. Nếu không, danh tiếng nghèo sẽ đi đến chất thải và không ai có lợi * sadface *. –
Bạn nói đúng, tôi sẽ –