Search code examples
androidandroid-imageandroid-gpuimageview

Image saved in sdcard shows only black screen using GPUImageView in android


I am developing one project which is used to apply some effects on the image which will be selected from gallery or captured through camera.

I have used the GPUImageView library for applying different effects on the images and also implemented the seekbar for increasing and decreasing the effects. Everything is working fine.

The problem is that now i want to save that effects applied image into sdcard. I have written the code for it and its working successfully but the image is saved is remained black. Whenever i check the image from the sdcard it does not show the modified image except black screen.

I do not know why its showing black image.

Here is the code where i have made the GPUImageView as drawingcachedenabled to true and saving it as image.

mGPUImageView.setDrawingCacheEnabled(true);
Bitmap bitmap = mGPUImageView.getDrawingCache();
String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath();
File fil = new File(path + "/MyPics");
fil.mkdir();
Random rand = new Random();
int aa = rand.nextInt();
String filename = "Photo" + aa+".jpg";
File newImg = new File(fil, filename);
    try {
        FileOutputStream out = new FileOutputStream(newImg);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {

}
    Toast.makeText(this,"File Saved at Path---->" + newImg.getAbsolutePath(),
                    Toast.LENGTH_LONG).show();
            mGPUImageView.setDrawingCacheEnabled(false);

Please help me to solve the issue.


Solution

  • I have resolved my issue by using the saveToPictures() method of GPUImageView.java. I simply converted the File newImg into the Uri and passed it to the Listener of OnPictureSavedListener class's onPictureSaved method as below:

            mGPUImageView.setDrawingCacheEnabled(true);
            File fil = new File(path);
            fil.mkdir();
            Random rand = new Random();
            int aa = rand.nextInt();
    
            mGPUImageView.setDrawingCacheEnabled(false);
             mGPUImageView.saveToPictures("MyPics", "Photo"+aa+".jpg", picSaveListener);
            picSaveListener.onPictureSaved(Uri.fromFile(newImg)); 
    
       OnPictureSavedListener picSaveListener = new OnPictureSavedListener() {
    
        @Override
        public void onPictureSaved(Uri uri) {
    
        }
    };