android - 图像保存到内部存储后在图库中显示图像
问题描述
我已经尝试了一切,但图像没有显示在画廊中,但它保存在内部存储中。我已经尝试了几个教程,但无法解决它。如何实现它以便它显示在图库中
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(FullScreenActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
//to get the image from the ImageView (say iv)
BitmapDrawable draw = (BitmapDrawable) fullscreenimage.getDrawable();
Bitmap bitmap = draw.getBitmap();
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/IslamicStatusPics");
Log.e("DIR", String.valueOf(dir));
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
try {
outStream = new FileOutputStream(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
try {
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
AlertDialog alertDialog = new AlertDialog.Builder(FullScreenActivity.this).create();
alertDialog.setTitle("Success");
alertDialog.setMessage("Image Saved to IslamicStatusPics Folder Successfully !");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/storage/emulated/0/IslamicStatusPics/"))));
// Toast.makeText(getApplicationContext(), "Image Saved to IslamicStatusPics Successfully !", Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
// intent.setData(Uri.fromFile(dir));
// sendBroadcast(intent);
}else {
requestStoragePermission();
}
请帮助我引导正确的道路。谢谢
解决方案
您可以使用保存的文件 uri 尝试以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(outFile)); //change here
activity.sendBroadcast(mediaScanIntent);
} else {
activity.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
推荐阅读
- oracle - 在 plsql 中加入更新语句
- javascript - 输入元素的值更改但未在浏览器中显示?
- git - 我可以在一个存储库上提交两个不同的存储库吗?
- c# - 使用无法连接到 Internet 的代理构建 .NET Core 应用程序
- hadoop - Nifi 1.8.0 - 使用 fetchHDFS 处理器时 nifi 中的线程终止
- python - 两个图中相同的轴顺序
- java - java文件和testng.xml中的多个类文件
- maven - 项目构建错误:不可解析的父 POM
- php - 未选择数据库 - php 和 phpmyadmin
- python - Python sql数据框数据类型转换拆分为日期和时间列