gpt4 book ai didi

android - 使用 MediaScannerConnection scanFile 扫描下载的图像文件

转载 作者:行者123 更新时间:2023-11-30 02:33:44 25 4
gpt4 key购买 nike

我正在开发一个应用程序,在该应用程序中我将图像保存到一个目录,但在我重新启动手机之前图像不会显示在图库中。

这是我的代码 fragment

public class SaveTask extends AsyncTask<String , String , String>
{
private Context context;
private ProgressDialog pDialog;
String image_url;
URL myFileUrl;
String myFileUrl1;
Bitmap bmImg = null;
File file ;

public SaveTask(Context context) {
this.context = context;
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub

super.onPreExecute();

pDialog = new ProgressDialog(context);
pDialog.setMessage("Downloading Image ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub

try {

myFileUrl = new URL(args[0]);
//myFileUrl1 = args[0];

HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
}
try {

String path = myFileUrl.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File (filepath.getAbsolutePath() + "/mydownloaddir/");
dir.mkdirs();
String fileName = idStr;
file = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(file);
bmImg.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();

}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}


@Override
protected void onPostExecute(String args) {
// TODO Auto-generated method stub
Toast.makeText(SlideImageActivity.this, "Image Saved Succesfully to Folder 'mydownloaddir'", Toast.LENGTH_SHORT).show();
pDialog.dismiss();
}
}

接下来我应该使用什么代码来使用 medisscanner 在图库中显示图像

我在这里有类似的东西但是无法正确使用它:

MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.i(TAG, "Scanned " + path);
}
});

请帮忙

最佳答案

使用

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));

//但它只适用于 API <19

第二种方法

private void scanFile(String path) {

MediaScannerConnection.scanFile(MainActivity.this,
new String[] { path }, null,
new MediaScannerConnection.OnScanCompletedListener() {

public void onScanCompleted(String path, Uri uri) {
Log.i("TAG", "Finished scanning " + path);
}
});
}

这样称呼

scanFile(yourFile.getAbsolutePath());

Also see this Answer

关于android - 使用 MediaScannerConnection scanFile 扫描下载的图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26912832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com