[英]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 下载管理器完成
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.