如果我使用以下代码保存图像,然后手动打开我的图库,图像显示正常。如果我尝试以编程方式打开图库,我会从图库中收到一条消息,说“没有缩略图”。
//save bitmap--------------------------------------------------
File myDir=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//media scan--------------------------------------------------
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(myDir.getAbsolutePath());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.getApplicationContext().sendBroadcast(mediaScanIntent);
//view in gallery (here is where my gallery gives "No Thumbnail" message)----
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+""),"image/*");
startActivity(viewIntent);