我正在尝试从运行PHP的服务器获取一个photosphere图像。我使用通用图像加载器库获取图像。然后,在我获得位图后,我将其保存到SD卡,然后在其他活动中,我尝试使用库打开photosphere图像。问题是,当我保存图像时,它被压缩形成6MB到只有不到1MB,这导致图库无法打开photosphere图像。有什么方法可以解决这个问题吗? (我想知道是否可以将位图直接传递到图库,或者有一种方法可以保存未压缩的图像)
代码:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(mHotelDetailResponseData.photosphere, mImage, defaultOptions, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
Log.d("Test","fail : " + failReason.getCause().getMessage());
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
Log.d("Test","complete");
writeBitmapToMemory(loadedImage);
mPhotoSphereButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("test","can continue");
Intent i = new Intent(getActivity(), PhotosphereActivity.class);
startActivityForResult(i,0);
}
});
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
Log.d("Test","progress:" +current+"/"+total);
}
});
private void writeBitmapToMemory(Bitmap bitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
Log.d("test","written to : " + Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
} catch (IOException e) {
e.printStackTrace();
Log.d("test","error : " + e.getMessage());
}
}
发送意图在图库中打开它的活动:(如果我打开未压缩的光球,则工作正常)
public class PhotosphereActivity extends BaseActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startPanorama();
}
private void startPanorama() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("com.google.android.gms", "com.google.android.gms.panorama.PanoramaViewActivity"));
intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "test.jpg"), "image/*");
startActivity(intent);
}
答案 0 :(得分:0)
private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
mPhotosphereFile = pictureFile;
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
private File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ mContext.getPackageName()
+ "/Files");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".png";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
Log.d("test","written to : " + mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}