如何在 Android(2.1 或更高版本)上获取设备(特别是 SD 卡)上的所有图像
我已经按照这里的教程进行操作: http: //androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html 还有另一个具有几乎相同的代码,并且我在不同设备上处理图像的方式方面遇到了一些问题/不一致。
我收集图像的代码是这样的:
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
imagecursor = managedQuery(uri, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID);
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor
.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(),
EvidenceImageView.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
我有一个运行 android 2.2 的模拟器,并且能够创建一个 sdcard 文件来安装它。我使用 adb 将一些图像复制到文件夹中并重新启动模拟器(我的应用程序没有显示)首先任何图像)..重新启动后,一些图像显示在我的应用程序中,但不是全部..然后,在我打开模拟器附带的图库应用程序后,我能够让我的图库加载所有图像就好了..不过我也有一个 HTC thunberbolt 并且上面有照片(在 /sdcard/DCIM/100MEDIA 下) 并且它根本不会在同一个应用程序中显示任何图像(指的是我正在开发的应用程序)。我没有将手机连接到计算机,所以我知道我的手机能够访问 SD 卡.. 另外,我尝试了手机附带的图库应用程序,我的照片/视频加载得很好。. HTC 手机附带的应用程序与普通 Android 手机附带的应用程序有点不同我相信..这让我想知道系统是否没有以同样的方式处理图像..
我对android开发是全新的,希望这会非常简单..我研究了一些其他应用程序,例如facebook,并且当我想上传照片时,它似乎直接带我进入 htc 提供的图库。也许我可以采取同样的方法(不完全确定如何)。 最终我希望用户能够选择多个图像并上传所有图像...... 我还找到了android附带的相机应用程序的源代码:
https://android.googlesource.com/platform/packages/apps/Camera/+/master/src/com/android/camera
这有点有用,但信息量很大此时要消化。
任何关于“这个东西是如何工作的”的解释都会很棒。但最主要的是,如何让我的图库显示所有缩略图(如有必要,创建缩略图)。或者,如何与系统图库交互。
I've followed the tutorial here: http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html
and also another one which has almost identical code, and I'm having some problems/inconsistencies in how images are processed on different devices.
My code for gathering the images is this:
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
imagecursor = managedQuery(uri, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID);
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor
.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(),
EvidenceImageView.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
I have an emulator running android 2.2 and was able to create an sdcard file to mount with it.. I copied some images into a folder using adb and restarted the emulator (my app didn't show any images at first).. After rebooting some of the images showed up in my app but not all.. Then after I open the Gallery application that comes with the emulator I was able to get my gallery to load all of the images just fine.. However I also have a HTC thunberbolt and i've got photos on it (under /sdcard/DCIM/100MEDIA)
and it will not display any images at all in the same app (referring to the one I'm working on).. I do not have the phone hooked up to the computer so I know there is no problem with my phone being able to access the sdcard.. Also I've tried the gallery application that came with my phone and my photos/videos load just fine in that.. The application that comes with HTC phones is a bit different than the one that comes with vanilla android phones I believe.. This makes me wonder if the system isn't handing images the same way..
I am brand new to android development and was hoping this would be pretty simple.. I looked into some other apps, such as facebook, and it appears to take me directly into the gallery provided by htc when I want to upload a photo.. Perhaps I could take this same approach (not totally sure how)..
Eventually I want the user to be able to select multiple images and upload all of them...
I also found the source code for the Camera app that comes with android:
https://android.googlesource.com/platform/packages/apps/Camera/+/master/src/com/android/camera
This is somewhat useful but quite a bit of information to digest at this point.
Any explanations of "how this stuff works" would be great. The main thing though is, how do I get my gallery to show all of the thumbnail images (creating thumbnails if necessary).. Or, how can I interface with the system gallery..
这有什么问题:
如何为我的应用程序从图库(SD 卡)中选择图像?
What's wrong with this:
How to pick an image from gallery (SD Card) for my app?