android - การดึงภาพจาก Gallery (ทีละภาพ)
โค้ดโดยปกติ (ไม่ใช่ library)
MainActivity.java
Image chooser library
Gandle in Android Studio
compile 'com.kbeanie:image-chooser-library:1.4.4@aar'
Offical Link
https://github.com/coomar2841/image-chooser-library
Usage and Sample
https://github.com/coomar2841/image-chooser-library/wiki/Usage-and-Sample-App
OnCreate
Override 2 Method
Full Code
public class MainActivity extends ActionBarActivity implements ImageChooserListener {
private ImageChooserManager imageChooserManager = new ImageChooserManager(MainActivity.this, ChooserType.REQUEST_PICK_PICTURE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
imageChooserManager.setImageChooserListener(MainActivity.this);
imageChooserManager.choose();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} // onCreate
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == ChooserType.REQUEST_PICK_PICTURE) {
imageChooserManager.submit(requestCode, data);
}
}
@Override
public void onImageChosen(final ChosenImage chosenImage) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (chosenImage != null) {
File imagepath = new File(chosenImage.getFilePathOriginal());
Bitmap bitmap_img = BitmapFactory.decodeFile(imagepath.getAbsolutePath());
// set image into imageview
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap_img);
// Other image size
//Log.e("getFileThumbnail", chosenImage.getFileThumbnail());
//Log.e("getFileThumbnailSmall", chosenImage.getFileThumbnailSmall());
}
}
});
} // onImageChosen
@Override
public void onError(String s) {
Log.e("Error", s.toString());
}
}