Save photosphere image uncompressed to storage

252 Views Asked by At

I'm trying to get a photosphere image from server which run PHP. I fetch the image using the universal image loader library. And then, after I got the bitmap, I save it to the sd card, and then on other activity, I try to open the photosphere image using the gallery. the problem is that when I saved the image, it is compressed form 6MB to only less than 1MB which causes the gallery cannot open the photosphere image. is there any way to solve this? (I was wondering if it is possible to pass the bitmap directly to gallery or there is a method to save the image uncompressed)

the code :

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

the activity that send an intent to open it in gallery : (works okay if I open the uncompressed 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);
}
1

There are 1 best solutions below

0
On BEST ANSWER

okay i've finally figure it out. the problem is that when I open the image using gallery, the intent type must be specifically define, not just "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;
}