Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I make Solution altranative for AsyncTask for saving images into storage. Master approval Change in libery upcoming version. #526

Open
chirag-deshwal opened this issue Jun 19, 2021 · 0 comments

Comments

@chirag-deshwal
Copy link

Future Task

What is the motivation?

Quality Apps

What kind of solution can be considered?

What do you want to discuss?

Solution alternative for AsyncTask. Change into your library code after testing is work's for me

Please add relevant labels
Gift for Master by Rahul Deshwal

Bug Reporting

Steps to Reproduce

Actual Results (include screenshots)

Expected Results (include screenshots)

URL

OS details

  • Device:
  • OS: Android 10+

Please add relevant labels

##Code
// Let's continue work with the alternative of AsyncTask

 /** I don't know what is the use of  { Callable<MyParasol>  } but is working without leaking memory 😉😜
    If you use   Callable<Void> so leaking memory 
    
    Request to the master Change the basic  and use it
    I don't know how to change the library inner file's code in my own project so  making a separate class for This function 
   */

`
public class ImageSaver {

public static class LongRunningTask implements Callable<MyParasol> {
    private final String fileName;
    private final String folderName;
    private final Handler mHandler;
    private final Bitmap image;
    private final OnPictureSavingListener listener;
    private final Context context;


    public LongRunningTask(Context context , String folderName , String fileName , Bitmap image, OnPictureSavingListener listener) {
        this.context = context;
        this.fileName = fileName;
        this.folderName = folderName;
        this.mHandler = new Handler();
        this.image = image;
        this.listener = listener;
    }

    @Override
    public MyParasol call() {
        // Continue your work task ....
        saveImage(folderName , fileName , image , context , listener , mHandler );
        return null;
    }
}

 public static class ImageSaverNewApi implements Callable<Void> {

    private final Executor executor = Executors.newSingleThreadExecutor(); // change according to your requirements
    private final Handler handler = new Handler(Looper.getMainLooper());

    public ImageSaverNewApi() {

    }

    @Override
    public Void call() {
        return null;
    }

    public interface Callback<R> {
        void onComplete(R result);
    }


    public <R> void executeAsync(Callable<R> callable, Callback<R> callback) {
        executor.execute(() -> {
            R result;
            try {
                result = callable.call();
                R finalResult = result;
                handler.post(() -> callback.onComplete(finalResult));
            } catch (Exception e) {
                e.printStackTrace();
            }

        });
    }


}


public static void saveImage(final String folderName,
                             final String fileName,
                             final Bitmap image ,
                             Context context ,
                             OnPictureSavingListener listener ,
                             Handler mHandler ) {

    // File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); --> Copy form GPUImageFilter.class
    // Save for files System PICTURES DIRECTORY

    File folderPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), folderName);
    File fileSavingPath = new File(folderPath, fileName);
    try {
        boolean isFolderExist = folderPath.exists();
        if (!folderPath.exists()) {
            isFolderExist =  folderPath.mkdirs();
        }
        if (isFolderExist) {
            image.compress(Bitmap.CompressFormat.JPEG, 100 , new FileOutputStream(fileSavingPath));
            MediaScannerConnection.scanFile( context ,
                    new String[] {
                     fileSavingPath.toString()
                    }, null,
                    (path1, uri) -> {
                        if (listener != null) {
                            mHandler.post(() -> listener.onImageSaved( fileSavingPath.toString()  , uri));
                        }
                    });
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        listener.onImageSavedFail(e);
    }
}



// I made This For callback When an image is successfully save 
public interface OnPictureSavingListener {

    /** @param mediaScannerUri - is Empty if folder hidden
     *  @param filePath - a location where is file saved in the phone
     *  */
    void onImageSaved(String filePath , Uri mediaScannerUri);
    void onImageSavedFail(Exception e);
}

public static class MyParasol {
    String fileName;
    Uri mediaScannerUri;

    public String getFileName() {
        return fileName;
    }

    public Uri getMediaScannerUri() {
        return mediaScannerUri;
    }
}

}

// Use In Your Activity and Fragment Class

 ImageSaver.OnPictureSavingListener onPictureSavingListener = new ImageSaver.OnPictureSavingListener() {
                        /**
                        * @param filePath where is an image saved Directory path with filename 
                        * @param mediaScannerUri is May be null if you save file in the Hidden folder          
                        * */
                       @Override
                       public void onImageSaved(String filePath , Uri mediaScannerUri) {
                         
                          // Your result you can do anything 
                          

                       }

                       @Override
                       public void onImageSavedFail(Exception e) {
                           e.fillInStackTrace();
                       }
                   };
new ImageSaver.ImageSaverNewApi().executeAsync(new ImageSaver.LongRunningTask(

context ,
"AppName/.FiltersThumbnail" , // Folder Name
modal.getFilterName() + ".jpg" , // File name
gpuImage.getBitmapWithFilterApplied() , // Bitmap
onPictureSavingListener // Listener
), result -> {
/// Empty because i don't the
});
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant