本想用cocos2d-x跨平台实现读取相册问题,但奈何没有相关接口,,只好发扬“自己动手,丰衣足食”的原则了,以下是在网上找的相关素材,转载过来,等有空的时候实现一下。
-----------------------------------------------andriod部分-------------------------------------------------------
首先是相册图片的获取:
private final String IMAGE_TYPE = "image/*";
private final int IMAGE_CODE = 0; //这里的IMAGE_CODE是自己任意定义的
//使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
getAlbum.setType(IMAGE_TYPE);
startActivityForResult(getAlbum, IMAGE_CODE);
//重写onActivityResult以获得你需要的信息
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode != RESULT_OK) { //此处的 RESULT_OK 是系统自定义得一个常量
Log.e(TAG,"ActivityResult resultCode error");
return;
}
Bitmap bm = null;
//外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
ContentResolver resolver = getContentResolver();
//此处的用于判断接收的Activity是不是你想要的那个
if (requestCode == IMAGE_CODE) {
try {
Uri originalUri = data.getData(); //获得图片的uri
bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); //显得到bitmap图片
这里开始的第二部分,获取图片的路径:
String[] proj = {MediaStore.Images.Media.DATA};
//好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = managedQuery(originalUri, proj, null, null, null);
//按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
//最后根据索引值获取图片路径
String path = cursor.getString(column_index);
}catch (IOException e) {
Log.e(TAG,e.toString());
}
}
}
源地址:http://blog.csdn.net/hugh114/article/details/5765332
-----------------------------------------------ios部分-------------------------------------------------------
怎么获取相册里的所有图片
- (void) fetchPhotosFromPhotoAlbum { NSLog(@"准备扫描图片库, 等待访问信号"); dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); NSLog(@"------ 扫描设置中(锁定扫描库)"); working = YES; //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableArray* tempUrlListArray = [[NSMutableArray alloc] init]; //临时url集合 ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ NSLog(@"error occour =%@", [myerror localizedDescription]); working = NO; dispatch_semaphore_signal(sem); //完成时调用 if (delegate && [delegate conformsToProtocol:@protocol(PhotoLibraryDelegate)]) { [delegate fetchPhotosFailure]; } }; ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){ //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; if (result!=NULL) { //we can get all the things in the defaultRepresentation such as size info in UTI } //NSLog(@"find: %@", url); //just fetching photos if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { NSURL *url = [[result defaultRepresentation] url]; [tempUrlListArray addObject:url]; } //[pool release]; }; ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){ if (group != nil) { [group enumerateAssetsUsingBlock:groupEnumerAtion]; } else { NSLog(@"------ 扫描结束"); working = NO; dispatch_semaphore_signal(sem); //完成时调用 if (delegate && [delegate conformsToProtocol:@protocol(PhotoLibraryDelegate)]) { [delegate fetchPhotosFinished:tempUrlListArray]; } [tempUrlListArray release];//释放临时url集合 } }; //异步的 [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupLibrary | ALAssetsGroupSavedPhotos usingBlock:libraryGroupsEnumeration failureBlock:failureblock]; NSLog(@"------ 扫描设置结束, 等待扫描任务启动"); //[pool release]; } |
源地址:http://tech.techweb.com.cn/thread-559289-1-1.html