在 Android 的图库或图像选择器中找不到动态创建的图像文件
Dynamically created image file cannot be found in Gallery or image picker in Android
我正在开发 Android 应用程序。在我的应用程序中,我从位图创建图像文件,然后将其保存到设备。从位图创建图像文件并将图像保存到设备的过程是可以的。问题是我在图库中找不到创建的图像。但是当我从文件管理器中搜索时,文件确实存在。
这是我的代码:
File file = null;
try{
String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
if(file.exists()){
file.delete();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
template.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Toast.makeText(context,"Templated saved to your device",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
如您所见,我将其保存在图片文件夹中。我也尝试将其保存在下载文件夹中。文件已成功保存到设备,但问题是图像未显示在图库中。但存在于文件管理器中。我怎样才能使图片在图库中可搜索?
试试这个:
File file = null;
try{
String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
if(file.exists())
{
file.delete();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
template.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Toast.makeText(context,"Templated saved to your device",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
保存文件后使用此代码
try {
MediaScannerConnection.scanFile(context,
new String[] { mPath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
} catch (Exception e) {
}
我正在开发 Android 应用程序。在我的应用程序中,我从位图创建图像文件,然后将其保存到设备。从位图创建图像文件并将图像保存到设备的过程是可以的。问题是我在图库中找不到创建的图像。但是当我从文件管理器中搜索时,文件确实存在。
这是我的代码:
File file = null;
try{
String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
if(file.exists()){
file.delete();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
template.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Toast.makeText(context,"Templated saved to your device",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
如您所见,我将其保存在图片文件夹中。我也尝试将其保存在下载文件夹中。文件已成功保存到设备,但问题是图像未显示在图库中。但存在于文件管理器中。我怎样才能使图片在图库中可搜索?
试试这个:
File file = null;
try{
String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
if(file.exists())
{
file.delete();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
template.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Toast.makeText(context,"Templated saved to your device",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
保存文件后使用此代码
try {
MediaScannerConnection.scanFile(context,
new String[] { mPath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
} catch (Exception e) {
}