在Android Studio中,我想用Java在我的位图对象中创建一个图像的缩略图,并将其存储在一个特定的文件夹中。

问题描述 投票:-1回答:1

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();
            }
       }
java android android-studio thumbnails fileoutputstream
1个回答
0
投票

你可以使用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)
     {}

 }
© www.soinside.com 2019 - 2024. All rights reserved.