Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Bitmap decodeBase64(String input,Context context) {
- byte[] decodedByte = Base64.decode(input, 0);
- Boolean isSDPresent = android.os.Environment
- .getExternalStorageState().equals(
- android.os.Environment.MEDIA_MOUNTED);
- File sdCardDirectory;
- if (isSDPresent) {
- // yes SD-card is present
- sdCardDirectory = new File(
- Environment
- .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
- "MySnaps");
- if (!sdCardDirectory.exists()) {
- if (!sdCardDirectory.mkdirs()) {
- Log.d("MySnaps", "failed to create directory");
- }
- }
- } else {
- // Sorry
- sdCardDirectory = new File(context.getCacheDir(),"");
- }
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
- .format(new Date());
- Random rand = new Random();
- // nextInt is normally exclusive of the top value,
- // so add 1 to make it inclusive
- int randomNum = rand.nextInt((1000 - 0) + 1) + 0;
- String nw = "mysnanp_" + timeStamp + randomNum+".txt";
- File image = new File(sdCardDirectory, nw);
- // Encode the file as a PNG image.
- FileOutputStream outStream;
- try {
- outStream = new FileOutputStream(image);
- outStream.write(input.getBytes());
- outStream.flush();
- outStream.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- Log.i("Compress bitmap path", image.getPath());
- return decodeFile(image); // BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
- }
- private static Bitmap decodeFile(File f){
- try {
- //Decode image size
- BitmapFactory.Options o = new BitmapFactory.Options();
- o.inJustDecodeBounds = true;
- BitmapFactory.decodeStream(new FileInputStream(f),null,o);
- //The new size we want to scale to
- final int REQUIRED_SIZE=70;
- //Find the correct scale value. It should be the power of 2.
- int scale=1;
- while(o.outWidth/scale>=REQUIRED_SIZE && o.outHeight/scale>=REQUIRED_SIZE)
- scale*=2;
- //Decode with inSampleSize
- BitmapFactory.Options o2 = new BitmapFactory.Options();
- o2.inSampleSize=scale;
- return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
- } catch (FileNotFoundException e) {}
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement