http回调时保存的图片:
void ImageHttpAdapter::onResponseListener(long lastLength, vector<char> *buf){
if(buf != NULL){
// FileUtils::saveImage2Files(imageUrl, buf); //方式1
FileUtils::saveImage2SDCard(imageUrl);
CCImage *image = new CCImage();
image->initWithImageData(buf->data(), buf->size());
image->saveToFile(FileUtils::getFileRealPath(imageUrl).c_str()); //方式2
CCTexture2D *t2d = new CCTexture2D();
t2d->initWithImage(image);
image->release();
(target->*asyncSel)(getPrivateData(), t2d);
}else{
(target->*asyncSel)(NULL, NULL);
}
}
方式1:
void FileUtils::saveImage2Files(string url, vector<char> *buffer){
string fileName = MD5(url).toString() + SUFIX;
string path = CCFileUtils::sharedFileUtils()->getWritablePath() + DIR_IMAGE;
if(CCFileUtils::sharedFileUtils()->isFileExist(path) == false){
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
mode_t processMask = umask(0);
mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
umask(processMask);
#endif
}
path += "/" + fileName;
if(CCFileUtils::sharedFileUtils()->isFileExist(path) == false){
string buf(buffer->begin(), buffer->end());
FILE *fp = fopen(path.c_str(), "wb+");
fwrite(buf.c_str(), 1, buffer->size(), fp);
fclose(fp);
}
}
读取图片文件:
CCTexture2D* FileUtils::getImageFromFiles(string url){
CCTexture2D *t2d = NULL;
do{
CCImage *image = new CCImage();
bool ret = false;
if(FileUtils::isFileExist(url) == true){
ret = image->initWithImageFile(getFileRealPath(url).c_str(), CCImage::kFmtJpg);
CC_BREAK_IF(!ret);
CCLog("读取成功");
}else{
return NULL;
}
t2d = new CCTexture2D();
t2d->initWithImage(image);
image->release();
break;
}while (0);
return t2d;
}
无论是用方式1, 还是方式2, 下载到手机里的图片, 大小正常, 但是图片就是黑色的, 如果直接加载网络图片就能正常显示.
读取出来的图片也是黑色的…
请问这是怎么回事啊???
奇怪的是:
1.直接用手机打开图片是黑色的
2.把图片拷贝电脑,再打开,正常.
3.在ios模拟器上,无论保存还是读取, 正常
4.用android代码加载到布局里, 正常
android代码:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
....
File root = new File(getFilesDir() + File.separator + "image");
root.listFiles().getAbsolutePath();
ImageView iv = new ImageView(act);
iv.setImageBitmap(BitmapFactory.decodeFile(root.listFiles().getAbsolutePath()));
setContentView(iv);
}
另:
cocos2dx版本2.2.5
用的android真机测试, 版本4.4.4