将未压缩的光球图像保存到存储

Save photosphere image uncompressed to storage

我正在尝试从运行 PHP 的服务器获取光球图像。我使用通用图像加载器库获取图像。然后,在我得到位图后,将其保存到 sd 卡,然后在其他 activity 上,我尝试使用图库打开光球图像。问题是当我保存图像时,它从 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());
    }
}

发送意图在图库中打开它的 activity:(如果我打开未压缩的 Photosphere 就可以正常工作)

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);
}

好的,我终于弄明白了。问题是当我使用图库打开图像时,必须明确定义意图类型,而不仅仅是 "image/*"

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;
}