截至目前,我已经在我的应用程序中使用提供的小部件创建了一个基本的画廊应用程序,我需要它来从手机中选择图片。这一切都很好,但在演示中非常缺乏。
我的手机上有几个应用程序可以做同样的事情,但它们以某种方式使用手机中已有的图库让用户选择图像。例如,FourSquare,当您选择要用作图片的图像时,它会加载图库并要求您选择图像。
这怎么可能?我在互联网上搜索了最后一对,但一无所获。
最佳答案
要从标准图库中获取图像,您可以执行以下操作:
private static final int MEDIA_IMAGE_REQUEST_CODE = 203948; // This can be any unique number you like
Intent getImageFromGalleryIntent =
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, MEDIA_IMAGE_REQUEST_CODE);
然后在用户选择一个图像后接收图像:
protected final void onActivityResult(final int requestCode, final int resultCode, final Intent i) {
super.onActivityResult(requestCode, resultCode, i);
if(resultCode == RESULT_OK) {
switch(requestCode) {
case MEDIA_IMAGE_REQUEST_CODE:
// Get the chosen images Uri
Uri imageUri = i.getData();
// Load the bitmap data from the Uri
// It is probably best to do this in an AsyncTask to avoid clogging up the Main Thread
break;
}
}
关于Android SDK - 引用手机的图库应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2959223/