简体   繁体   English

下载的图像未显示在画廊 android

[英]downloaded image not showing in galllery android

I am trying to download an image from the URL and save it in android.我正在尝试从 URL 下载图像并将其保存在 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.这是我的代码:API 版本是 30。请任何帮助将不胜感激谢谢。

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.我已经更新了我的问题,这是我除了导入之外的完整源代码。

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.由于图像尚未下载,您应该注册一个BroadcastReceiver以了解下载何时完成,然后运行媒体扫描仪。

See this: Android download manager completed看到这个: Android 下载管理器完成


Edit:编辑:

If the target api level is 29 , notice that Environment.getExternalStorageDirectory() is deprecated and not to be used.如果目标 api 级别为29 ,请注意Environment.getExternalStorageDirectory()已弃用且不可使用。 ( Reference ) 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 图片按钮可打开手机图库并在Android上显示图片 - Image Button to open phone galllery and display picture on Android 下载的图片显示在Android Studio中,但不显示在图库中 - Downloaded image showing in android studio but not gallery 使用Android通用图像加载器下载的图像显示方向错误 - Downloaded images are showing wrong orientation using the Android universal image loader Android 显示图库中的图像,需要从 Internet 下载 - Android showing Image in Gallery which needs to be downloaded from Internet Android:下载的图片未显示在ListView中 - Android: Downloaded Images not showing in ListView Android:下载实际图像时,在图库的ImageView中显示默认图像 - Android: showing default image in gallery's ImageView while actual image is downloaded Android:图像已下载但未显示在imageview中 - Android: Image downloaded but not displayed in imageview 在android中播放下载的Gif图像 - play downloaded Gif image in android 显示进度条,直到从服务器下载图像 - showing progressbar until image downloaded from server 使用Cordova FileTransfer下载的图像未显示在图库中 - Image downloaded with Cordova FileTransfer is not showing up in Gallery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM