Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Proper "View Images in Gallery" Button Functionality #1088

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import swati4star.createpdf.util.PermissionsUtils;
import swati4star.createpdf.util.RealPathUtil;
import swati4star.createpdf.util.StringUtils;
import swati4star.createpdf.util.ImageUtils;

import static android.app.Activity.RESULT_OK;
import static swati4star.createpdf.util.Constants.BUNDLE_DATA;
Expand All @@ -72,6 +73,7 @@ public class PdfToImageFragment extends Fragment implements BottomSheetPopulate,
private Context mContext;
private PDFUtils mPDFUtils;
private String[] mInputPassword;
private ImageUtils mImageUtils;

@BindView(R.id.lottie_progress)
LottieAnimationView mLottieProgress;
Expand Down Expand Up @@ -109,24 +111,30 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
ButterKnife.bind(this, rootView);
mOperation = getArguments().getString(BUNDLE_DATA);
mSheetBehavior = BottomSheetBehavior.from(mLayoutBottomSheet);
mSheetBehavior.setBottomSheetCallback(new BottomSheetCallback(mUpArrow, isAdded()));
mSheetBehavior.addBottomSheetCallback(new BottomSheetCallback(mUpArrow, isAdded()));
mLottieProgress.setVisibility(View.VISIBLE);
mBottomSheetUtils.populateBottomSheetWithPDFs(this);
resetView();
getRuntimePermissions();
return rootView;
}


@OnClick(R.id.viewImagesInGallery)
void onImagesInGalleryClick() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imagesUri = Uri.parse("content:///storage/emulated/0/PDFfiles/");
intent.setDataAndType(imagesUri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
for (int i = 0; i < mOutputFilePaths.size(); i++) {
Uri imagesUri = Uri.fromFile(new File(mOutputFilePaths.get(i)));
Uri imagesRealUri = mFileUtils.getContent(imagesUri);
mImageUtils.saveImgToGallery(mOutputFilePaths.get(i), mContext);
intent.setDataAndType(imagesRealUri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
}


/**
* called when user chooses to share generated images
*/
Expand Down Expand Up @@ -229,6 +237,7 @@ public void onAttach(Context context) {
mActivity = (Activity) context;
mMorphButtonUtility = new MorphButtonUtility(mActivity);
mFileUtils = new FileUtils(mActivity);
mImageUtils = ImageUtils.getInstance();
mBottomSheetUtils = new BottomSheetUtils(mActivity);
mContext = context;
mPDFUtils = new PDFUtils(mActivity);
Expand Down Expand Up @@ -336,7 +345,7 @@ public boolean checkSheetBehaviour() {
***/
private void getRuntimePermissions() {
PermissionsUtils.getInstance().requestRuntimePermissions(this,
WRITE_PERMISSIONS,
REQUEST_CODE_FOR_WRITE_PERMISSION);
WRITE_PERMISSIONS,
REQUEST_CODE_FOR_WRITE_PERMISSION);
}
}
39 changes: 36 additions & 3 deletions app/src/main/java/swati4star/createpdf/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package swati4star.createpdf.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand All @@ -12,6 +14,7 @@
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.provider.MediaStore;

import androidx.core.content.FileProvider;

import com.afollestad.materialdialogs.MaterialDialog;
Expand Down Expand Up @@ -232,7 +235,6 @@ public static String getFileName(String path) {
return index < path.length() ? path.substring(index + 1) : null;
}


/**
* Extracts file name from the URI
*
Expand Down Expand Up @@ -363,9 +365,10 @@ String getUniqueFileName(String fileName) {
* Opens a Dialog to select a filename.
* If the file under that name already exists, an overwrite dialog gets opened.
* If the overwrite is cancelled, this first dialog gets opened again.
*
* @param preFillName a prefill Name for the file
* @param ext the file extension
* @param saveMethod the method that should be called when a filename is chosen
* @param ext the file extension
* @param saveMethod the method that should be called when a filename is chosen
*/
public void openSaveDialog(String preFillName, String ext, Consumer<String> saveMethod) {

Expand All @@ -387,4 +390,34 @@ public void openSaveDialog(String preFillName, String ext, Consumer<String> save
}
}).show();
}

/***
* Converts URI to actual decoded URI storing new image content
*
* @param uri: source URI starts with "file://"
* @return generated URI starts with "content://"
*/
public Uri getContent(Uri uri) {
if (!uri.getScheme().equals("file")) {
return uri;
}
if (uri.getEncodedPath() == null) {
return uri;
}
ContentResolver resolver = this.mContext.getContentResolver();
String buffer = "(" + MediaStore.Images.ImageColumns.DATA + "='" + Uri.decode(uri.getEncodedPath()) + "')";
@SuppressLint("Recycle") Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns._ID}, buffer, null, null);
int index = 0;
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
index = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID));
}
if (index == 0) {
return uri;
}
Uri newUri = Uri.parse("content://media/external/images/media/" + index);
if (newUri != null) {
uri = newUri;
}
return uri;
}
}
35 changes: 34 additions & 1 deletion app/src/main/java/swati4star/createpdf/util/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package swati4star.createpdf.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand All @@ -13,9 +16,12 @@
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import androidx.fragment.app.Fragment;

import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
Expand Down Expand Up @@ -278,5 +284,32 @@ private static boolean checkIfBitmapIsWhite(Bitmap bitmap) {
return bitmap.sameAs(whiteBitmap);
}

public ContentValues getImageContentValues(File paramFile, long paramLong) {
ContentValues localContentValues = new ContentValues();
localContentValues.put("title", paramFile.getName());
localContentValues.put("_display_name", paramFile.getName());
localContentValues.put("mime_type", "image/jpeg");
localContentValues.put("datetaken", paramLong);
localContentValues.put("date_modified", paramLong);
localContentValues.put("date_added", paramLong);
localContentValues.put("orientation", 0);
localContentValues.put("_data", paramFile.getAbsolutePath());
localContentValues.put("_size", paramFile.length());
return localContentValues;
}

public void saveImgToGallery(String imageFile, Context context) {
try {
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = getImageContentValues(new File(imageFile), System.currentTimeMillis());
resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
Intent intent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
intent.setData(Uri.fromFile(new File(imageFile)));
context.sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
}
}


}
}