I am trying to load all the images that I have onto the phones SD card when the app is created. I then have a button and when I click the button, the next image should show.
So far I have included this code in the method, however I don't know how to add multiple images into the folder. 方法中,但是我不知道如何将多个图像添加到文件夹中。
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "uct.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
At the moment I have an array of of image IDs, these images are stored in the Drawable folder at the moment.
int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Then I have an method that increments a private index variable to set the Image Resource like so (here imgView is the ImageView). 方法,可以增加一个私有索引变量来像这样设置Image Resource(此处imgView是ImageView)。
imgView.setImageResource(images[current_image_index]);
Firstly, I don't know how to add multiple images to the SD card in the onCreate() method. Also I am not sure what the best way to go about loading the 'correct' image from the SD when the button is clicked.
I hope this makes sense, please remark if anything needs to be clarified.
If i were in your case,
Create the ViewPager to show all images.
And you can get all images url using this code.
String[] STAR = {"*"}; ArrayList<String> imageUri = new ArrayList<>(); Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , STAR, null, null, null); if (cursor != null) { if (cursor.moveToFirst()) { do { String path = cursor.getString(cursor .getColumnIndex(MediaStore.Images.Media.DATA)); Log.i("Path", path); imageUri.add(path); getDate(path); } while (cursor.moveToNext()); } }
You can pass the imageUri ArrayList to Viewpager adapter.At viewPager adpter you can use any image library to load image. In case of Picasso .
Picasso.with(context) .load(uri) .resize(150,150) .centerCrop() .into(imageView);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.