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

fixed the bug that Gallery picker option is not showing #53

Merged
merged 6 commits into from
Feb 11, 2021
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Fixed
- Null CompressFormat [#44](https://github.com/CanHub/Android-Image-Cropper/issues/44)
- [Galley option not showing](https://github.com/CanHub/Android-Image-Cropper/issues/20)
- [Camera option not showing](https://github.com/CanHub/Android-Image-Cropper/issues/52)

## [2.0.3] - 27/01/21
Versions `2.0.1` and `2.0.2` are similar, issues with jitpack.
Expand Down
10 changes: 10 additions & 0 deletions cropper/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.canhub.cropper">
<queries>
<intent>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.OPENABLE"/>
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
<application>
<provider
android:name=".CropFileProvider"
Expand Down
66 changes: 46 additions & 20 deletions cropper/src/main/java/com/canhub/cropper/CropImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
import android.os.Parcelable;
import android.provider.MediaStore;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -51,6 +46,12 @@
import com.canhub.cropper.common.CommonValues;
import com.canhub.cropper.common.CommonVersionCheck;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Helper to simplify crop image work like starting pick-image acitvity and handling camera/gallery
* intents.<br>
Expand Down Expand Up @@ -153,7 +154,6 @@ public static Bitmap toOvalBitmap(@NonNull Bitmap bitmap) {
*
* @param activity the activity to be used to start activity from
*/
// TODO Issue #20
public static void startPickImageActivity(@NonNull Activity activity) {
activity.startActivityForResult(
getPickImageChooserIntent(activity), PICK_IMAGE_CHOOSER_REQUEST_CODE);
Expand Down Expand Up @@ -210,18 +210,10 @@ public static Intent getPickImageChooserIntent(
allIntents.addAll(getCameraIntents(context, packageManager));
}

allIntents.add(getGalleryIntent(Intent.ACTION_GET_CONTENT, includeDocuments));

Intent target;
if (allIntents.isEmpty()) {
target = new Intent();
} else {
target = allIntents.get(allIntents.size() - 1);
allIntents.remove(allIntents.size() - 1);
}
allIntents.addAll(getGalleryIntents(packageManager, Intent.ACTION_GET_CONTENT, includeDocuments));

// Create a chooser from the main intent
Intent chooserIntent = Intent.createChooser(target, title);
Intent chooserIntent = Intent.createChooser(allIntents.remove(allIntents.size()-1), title);

// Add all other intents
chooserIntent.putExtra(
Expand Down Expand Up @@ -272,20 +264,54 @@ public static List<Intent> getCameraIntents(
allIntents.add(intent);
}

// Just in case queryIntentActivities returns emptyList
if (allIntents.isEmpty()) {
allIntents.add(captureIntent);
}

return allIntents;
}


/**
* Get all Gallery intents for getting image from one of the apps of the device that handle
* images.
*/
public static Intent getGalleryIntent(String action, boolean includeDocuments) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(action);
public static List<Intent> getGalleryIntents(
@NonNull PackageManager packageManager, String action, boolean includeDocuments) {
List<Intent> intents = new ArrayList<>();
Intent galleryIntent = new Intent(action);
galleryIntent.setType(includeDocuments ? "*/*" : "image/*");
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);

return galleryIntent;
List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
if (CommonVersionCheck.INSTANCE.isAtLeastQ29() && listGallery.size() > 2) {
// Workaround for the bug that only 2 items are shown in Android Q
// // https://issuetracker.google.com/issues/134367295
// Trying to pick best match items
Collections.sort(listGallery, (o1, o2) -> {
final String packageName = o1.activityInfo.packageName;
if (packageName.contains("photo")) return -1;
if (packageName.contains("gallery")) return -1;
if (packageName.contains("album")) return -1;
if (packageName.contains("media")) return -1;
return 0;
});
listGallery = listGallery.subList(0, 2);
}
for (ResolveInfo res : listGallery) {
Intent intent = new Intent(galleryIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
intents.add(intent);
}

// Just in case queryIntentActivities returns emptyList
if (intents.isEmpty()) {
intents.add(galleryIntent);
}

return intents;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ open class CropImageOptions : Parcelable {
}

companion object {

@JvmField
val CREATOR: Parcelable.Creator<CropImageOptions?> =
object : Parcelable.Creator<CropImageOptions?> {
override fun createFromParcel(parcel: Parcel): CropImageOptions? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,16 @@ internal class CropImageViewFragment :
OptionsDialogBottomSheet.show(childFragmentManager, options, this)
}

// TODO Issue #20
// binding.searchImage.setOnClickListener {
// context?.let { ctx ->
// if (CropImage.isExplicitCameraPermissionRequired(ctx)) {
// requestPermissions(
// arrayOf(Manifest.permission.CAMERA),
// CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE
// )
// } else activity?.let { CropImage.startPickImageActivity(it) }
// }
// }
binding.searchImage.setOnClickListener {
context?.let { ctx ->
if (CropImage.isExplicitCameraPermissionRequired(ctx)) {
requestPermissions(
arrayOf(Manifest.permission.CAMERA),
CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE
)
} else context?.let { context -> CropImage.startPickImageActivity(context, this) }
}
}

binding.reset.setOnClickListener {
binding.cropImageView.apply {
Expand Down Expand Up @@ -157,9 +156,12 @@ internal class CropImageViewFragment :
binding.cropImageView.setOnCropImageCompleteListener(null)
}

override fun onSetImageUriComplete(view: CropImageView, uri: Uri, error: Exception) {
Log.e("AIC", "Failed to load image by URI", error)
Toast.makeText(activity, "Image load failed: " + error.message, Toast.LENGTH_LONG).show()
override fun onSetImageUriComplete(view: CropImageView, uri: Uri, error: Exception?) {
if (error != null) {
Log.e("AIC", "Failed to load image by URI", error)
Toast.makeText(activity, "Image load failed: " + error.message, Toast.LENGTH_LONG)
.show()
}
}

override fun onCropImageComplete(view: CropImageView, result: CropResult) {
Expand Down
23 changes: 11 additions & 12 deletions sample/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@
app:pressedTranslationZ="@dimen/elevation_fab_pressed"
tools:ignore="ContentDescription" />

<!-- Todo Issue #20 -->
<!-- <com.google.android.material.floatingactionbutton.FloatingActionButton-->
<!-- android:id="@+id/searchImage"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_margin="@dimen/keyline_x4"-->
<!-- android:elevation="@dimen/elevation_fab_resting_or_snackbar"-->
<!-- android:src="@drawable/ic_mage_search_24"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:pressedTranslationZ="@dimen/elevation_fab_pressed"-->
<!-- tools:ignore="ContentDescription" />-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/searchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/keyline_x4"
android:elevation="@dimen/elevation_fab_resting_or_snackbar"
android:src="@drawable/ic_mage_search_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:pressedTranslationZ="@dimen/elevation_fab_pressed"
tools:ignore="ContentDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>