Advertisement
Guest User

Base64 Decoding

a guest
Apr 12th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. public static Bitmap decodeBase64(String input,Context context) {
  2.         byte[] decodedByte = Base64.decode(input, 0);
  3.        
  4.        
  5.         Boolean isSDPresent = android.os.Environment
  6.                 .getExternalStorageState().equals(
  7.                         android.os.Environment.MEDIA_MOUNTED);
  8.  
  9.         File sdCardDirectory;
  10.         if (isSDPresent) {
  11.             // yes SD-card is present
  12.              sdCardDirectory = new File(
  13.                         Environment
  14.                                 .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
  15.                         "MySnaps");
  16.  
  17.                 if (!sdCardDirectory.exists()) {
  18.                     if (!sdCardDirectory.mkdirs()) {
  19.                         Log.d("MySnaps", "failed to create directory");
  20.  
  21.                     }
  22.                 }
  23.         } else {
  24.             // Sorry
  25.             sdCardDirectory = new File(context.getCacheDir(),"");
  26.         }
  27.  
  28.        
  29.  
  30.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
  31.                 .format(new Date());
  32.        
  33.         Random rand = new Random();
  34.  
  35.         // nextInt is normally exclusive of the top value,
  36.         // so add 1 to make it inclusive
  37.         int randomNum = rand.nextInt((1000 - 0) + 1) + 0;
  38.  
  39.         String nw = "mysnanp_" + timeStamp + randomNum+".txt";
  40.         File image = new File(sdCardDirectory, nw);
  41.  
  42.  
  43.  
  44.         // Encode the file as a PNG image.
  45.         FileOutputStream outStream;
  46.         try {
  47.  
  48.            
  49.             outStream = new FileOutputStream(image);
  50.             outStream.write(input.getBytes());
  51.  
  52.             outStream.flush();
  53.             outStream.close();
  54.         } catch (FileNotFoundException e) {
  55.             e.printStackTrace();
  56.         } catch (IOException e) {
  57.             e.printStackTrace();
  58.         }
  59.        
  60.        
  61.         Log.i("Compress bitmap path", image.getPath());
  62.        
  63.         return decodeFile(image); // BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
  64.     }
  65.    
  66.     private static Bitmap decodeFile(File f){
  67.         try {
  68.             //Decode image size
  69.             BitmapFactory.Options o = new BitmapFactory.Options();
  70.             o.inJustDecodeBounds = true;
  71.             BitmapFactory.decodeStream(new FileInputStream(f),null,o);
  72.  
  73.             //The new size we want to scale to
  74.             final int REQUIRED_SIZE=70;
  75.  
  76.             //Find the correct scale value. It should be the power of 2.
  77.             int scale=1;
  78.             while(o.outWidth/scale>=REQUIRED_SIZE && o.outHeight/scale>=REQUIRED_SIZE)
  79.                 scale*=2;
  80.  
  81.             //Decode with inSampleSize
  82.             BitmapFactory.Options o2 = new BitmapFactory.Options();
  83.             o2.inSampleSize=scale;
  84.             return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  85.         } catch (FileNotFoundException e) {}
  86.         return null;
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement