Skip to content

Commit

Permalink
add image file write method for pre-Q
Browse files Browse the repository at this point in the history
  • Loading branch information
antest1 committed Mar 17, 2020
1 parent 5cc40c5 commit 979faef
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions app/src/main/java/com/antest1/gotobrowser/Helpers/KcUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Base64;
Expand Down Expand Up @@ -382,7 +385,13 @@ public void run() {
String image = data.substring(data.indexOf(",") + 1);
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Uri fileUri = writeImageFile(context, filename, decodedImage);
Uri fileUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
fileUri = writeImageFile(context, filename, decodedImage);
} else {
fileUri = writeImageFileOld(context, filename, decodedImage);
}

Log.e("GOTO-DURL-P", "Path: " + fileUri.toString());
Log.e("GOTO-DURL-P", "Image Size: " + decodedImage.getWidth() + "x" + decodedImage.getHeight());
activity.runOnUiThread(() -> activity.showScreenshotNotification(decodedImage, fileUri));
Expand All @@ -401,7 +410,6 @@ public static Uri writeImageFile(Context context, String filename, Bitmap bitmap

ContentResolver contentResolver = context.getContentResolver();
Uri item = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
item.toString();
try {
ParcelFileDescriptor pd = contentResolver.openFileDescriptor(item, "w", null);
if (pd != null) {
Expand All @@ -426,5 +434,33 @@ public static Uri writeImageFile(Context context, String filename, Bitmap bitmap
}
return null;
}

public static Uri writeImageFileOld(Context context, String filename, Bitmap bitmap) {
String root = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/GotoBrowser");
myDir.mkdirs();
File file = new File (myDir, filename);
Log.e("GOTO", file.getAbsolutePath());

try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes.toByteArray());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, filename.concat(".png"));
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());

ContentResolver contentResolver = context.getContentResolver();
Uri item = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
return item;
}
}

0 comments on commit 979faef

Please sign in to comment.