0

I am trying to download an image from the URL and save it in android. but I am facing one problem here downloaded images are not showing in the gallery. I tried previously asked questions on stack overflow they didn't work for me. here is my code: API version is 30. please any help will be appreciated Thanks.

package com.example.testscripts;
/imports/
public class MainActivity extends AppCompatActivity {
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        progressBar = findViewById(R.id.progressbar);

        Button clickbtn = (Button) findViewById(R.id.button);

        clickbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText ein = (EditText)findViewById(R.id.urltext);
                Content content = new Content();
                content.execute(String.valueOf(ein.getText()));
            }
        });
    }
    
    public void downloadFile(String imgurls) throws IOException {
        String imageURL = imgurls;
        DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse(imgurls);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false).setTitle("Demo")
                .setDescription("someting")
                .setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, "hello.jpg");
        mgr.enqueue(request);
        File fles = new File(Environment.DIRECTORY_DOWNLOADS,"hello.jpg");
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(fles.getAbsolutePath())));
    }

    private class Content extends AsyncTask<String, Void, Void>  {
        private String val1;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
            progressBar.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));
        }
        @Override
        public void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressBar.setVisibility(View.GONE);
            progressBar.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));
        }
        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected Void doInBackground(String... voids) {
            try {
                val1 = (String) voids[0];
                String urls = val1;
                downloadFile(urls);
            } catch (IOException e){
                e.printStackTrace();
            }
            return null;
        }
    }
}

I have updated my question this is my full source code beside imports.

1 Answers1

0

It seems like the problem is that you are calling the media scanner exactly after enqueuing the download request. Since the image is not downloaded yet, you should register a BroadcastReceiver to know when the download gets finished, then run the media scanner.

See this: Android download manager completed


Edit:

If the target api level is 29, notice that Environment.getExternalStorageDirectory() is deprecated and not to be used. (Reference)

aminography
  • 21,986
  • 13
  • 70
  • 74