在
Android Studio
,使用Java
我想创建一个thumbnail
的图像,我有在我的Bitmap Object b
我想把图像存储在Nice/Data/A.jpg
缩略图应该有相同的名称,但目录应该是
Nice/Thumbnail/A.(Whatever Extension if possible then JPEG same as earlier)
//There is another class where i have stored the image in - Bitmap b;
//In this method - add i just want to store them in the respective folder
public void add()
{
File path = Environment.getExternalStorageDirectory();
File dir = new File(path.getAbsolutePath() + "/NICE/Data/");
dir.mkdirs();
// in this dir dirThumb the location is where i want to store thumbnail
File dirThumb = new File(path.getAbsolutePath() + "/NICE/Data/Thumbnails/");
dirThumb.mkdirs();
String Imgname = "A.jpg";
File file = new File(dir, Imgname);
OutputStream out;
try {
out = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 100, out);
//Here i want to add the code to Save Thumbnail of the image store in Bitmap b
Toast.makeText(this, " Successfully Added", Toast.LENGTH_SHORT).show();
out.flush();
out.close();
} catch (Exception e) {
Toast.makeText(this, "Exception" + e, Toast.LENGTH_SHORT).show();
}
}
你可以使用ThumbnailUtils.extractThumbnail从位图soruce图像中获取缩略图。
int height=64,width=64;
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(sourceBitmapImage, width, height);
得到位图缩略图后,再根据自己的意愿将其存储在任何文件夹中。
注意:你可以将这个缩略图存储在文件夹
NiceThumbnailA使用与您保存图片相同的代码。
private void ExtractAndSaveBitmapThumbnail(Bitmap bitmap)
{
//Extract Bitmap thumbnail from bitmap image object
int height=64,width=64;
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(sourceBitmapImage, width, height);
//Save Now to sepcific path
// Get the external storage directory path
File path = Environment.getExternalStorageDirectory();
try{
//create a file/directory
File dir=new File(path+"/Thumbnails/");
dir.mkdirs();
// Create a file to save the image
File file = new File(dir, "A"+".jpg");
//create a writeable sink for bytes
OutputStream stream = null;
//FileOutputStream will write bytes to a file
stream = new FileOutputStream(file);
//convert bitmap to compressed version
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
// flush (write) the buffered data
stream.flush();
//close writer
stream.close();
}catch(Exception ex)
{}
}