i have this code to download images from url but it doesnt save images in storage . can anyone fix it for me please [on hold]

34
September 03, 2019, at 12:40 PM

i have this code to download images from url but it doesnt save images in storage . can anyone fix it for me please . iam a beginner so excuse me for my mistakes .

i just add this to be able to post my question (jdcnbsiuvbuebvuebrvurbfbehcdhfjehbfebfybweyfbyewbdwebfycvevftegyewcbuwebchgmvoifdnvinuivnufvbuirenuverniufjuievnfnvhfbjtuingjfdnvjkbfdubgfjnjkndivbreuinmclkscnsdnc)

first class :

private void saveToFile(final int handlerCode) {
        currentHandlerCode = handlerCode;
        SaveImageRequest saveImageRequest = WallyApplication
                .getDataProviderInstance()
                .downloadImageIfNeeded(
                        imagePage.imagePath(),
                        pageUri.getLastPathSegment(),
                        getResources().getString(R.string.notification_title_image_saving));
if (saveImageRequest.getDownloadID() != null){
            WallyApplication.getDownloadIDs().put(saveImageRequest.getDownloadID(), pageUri.getLastPathSegment());
        } else {
            handleSavedImageData(saveImageRequest.getFilePath());
        }
    }

private void saveImage(boolean notifyUser) {
        if (notifyUser) {
            uiHandler.sendEmptyMessage(MSG_IMAGE_REQUEST_SAVING);
        }
        Message msgObj = backgroundHandler.obtainMessage();
        msgObj.what = MSG_SAVE_TO_FILE;
        msgObj.arg1 = MSG_IMAGE_REQUEST_SAVED;
        backgroundHandler.sendMessage(msgObj);
    }

private void handleSavedImageData(Uri filePath) {
        if (filePath != null && filePath.getPath() != null) {
            Message msgObj = uiHandler.obtainMessage();
            msgObj.what = currentHandlerCode;
            msgObj.obj = filePath;
            uiHandler.sendMessage(msgObj);
            MediaScannerConnection.scanFile(getApplicationContext(),
                    new String[]{filePath.getPath()},
                    null,
                    new MediaScannerConnection.MediaScannerConnectionClient() {
                        @Override
                        public void onMediaScannerConnected() {
 }
@Override
                        public void onScanCompleted(String path, Uri uri) {
                            getApplication().sendBroadcast(new Intent(FileReceiver.GET_FILES));
   }

 }
  );
 }
 }

second class :

public class SaveImageRequest {
    private Long downloadID;
    private Uri filePath;
    public SaveImageRequest(Long downloadID) {
        this.downloadID = downloadID;
        filePath = null;
    }
    public SaveImageRequest(Uri filePath) {
        this.filePath = filePath;
        downloadID = null;
    }
    public Long getDownloadID() {
        return downloadID;
    }
    public void setDownloadID(Long downloadID) {
        this.downloadID = downloadID;
    }
    public Uri getFilePath() {
        return filePath;
    }
    public void setFilePath(Uri filePath) {
        this.filePath = filePath;
    }


}

third class :

public class WallyApplication extends Application {
public static DataProvider getDataProviderInstance(){
        if (dataProvider == null){
            dataProvider = new DataProvider(getContext(), new ExceptionReporter.OnReportListener() {
                @Override
                public void report(Class fromClass, String reason, String exceptionMessage) {
                    if (getDataProviderInstance()
                            .getSharedPreferencesDataProviderInstance()
                            .hasUserApprovedCrashLogging() ==
                            SharedPreferencesDataProvider.CRASH_LOGGING_APPROVED) {
                        String message = "Class: " + fromClass.getName() + ", reason: " + reason +
                                ", exceptionMessage: " + exceptionMessage;
                        Crashlytics.log(message);
                    }
                }
            });
        }
        return dataProvider;
    }
public static HashMap<Long, String> getDownloadIDs() {
        if (pairedDownloadIds == null) {
            pairedDownloadIds = new HashMap<Long, String>();
        }
        return pairedDownloadIds;
    }
}

last class :

public SaveImageRequest downloadImageIfNeeded(Uri path, String filename, String notificationTitle){

        FileManager fileManager = new FileManager();
        if (fileManager.fileExists(filename)){
            File file = fileManager.getFile(filename);
            Uri fileUri = Uri.fromFile(file);
            return new SaveImageRequest(fileUri);
        } else {

            String type = ".png"; //fallback to ".png"
            if (path.toString().lastIndexOf(".") != -1) { //-1 means there are no punctuations in the path
                type = path.toString().substring(path.toString().lastIndexOf("."));
            }
            DownloadManager.Request request = new DownloadManager.Request(path);
            request.setTitle(notificationTitle);
            request.setVisibleInDownloadsUi(false);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
            request.allowScanningByMediaScanner();
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "/Wally/" + filename + type);
            return new SaveImageRequest(downloadManager.enqueue(request));

        }

    }
READ ALSO
Implementing search with multiple fragments MVVM

Implementing search with multiple fragments MVVM

I am trying to build an application which has multiple tabs in single Activity (using TabLayout and fragments for each tab)My question is how to properly transfer search query from activity's SearchView to the corresponding selected fragment

19
How to query all of the saved phone numbers, including those exported from external apps, in Android?

How to query all of the saved phone numbers, including those exported from external apps, in Android?

I'm trying to query all of the phone numbers for a contact on Android, currently I'm showing the contacts using the following method:

39
Change the item index in an arraylist

Change the item index in an arraylist

I have a List<POJO> with three PackagenamesSo if I "click" on the second item com

44
Unity IL2CPP Android build crashes Mac

Unity IL2CPP Android build crashes Mac

When trying to build a Google App Bundle, every time the process gets to the step that says Building Native Binaries With IL2CPP I get a “Your system has run out of application memory” error and my MacBook reboots

24