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

Solve some issues #755

Open
wants to merge 6 commits into
base: develop
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
2 changes: 2 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.yalantis.ucrop.sample">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
tools:replace="android:allowBackup"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
Expand Down
44 changes: 38 additions & 6 deletions sample/src/main/java/com/yalantis/ucrop/sample/ResultActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand All @@ -26,6 +27,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.time.Instant;
import java.util.Calendar;
import java.util.List;

Expand All @@ -36,6 +38,8 @@
import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider;

import lib.folderpicker.FolderPicker;

import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

Expand Down Expand Up @@ -94,7 +98,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_download) {
saveCroppedImage();
} else if (item.getItemId() == android.R.id.home) {
onBackPressed();
//onBackPressed();
//Toast.makeText(ResultActivity.this, "Back", Toast.LENGTH_LONG).show();
Intent sample_activity = new Intent(this, SampleActivity.class);
sample_activity.putExtra("back", true);
startActivity(sample_activity);
}
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -126,22 +134,46 @@ private void saveCroppedImage() {
Uri imageUri = getIntent().getData();
if (imageUri != null && imageUri.getScheme().equals("file")) {
try {
copyFileToDownloads(getIntent().getData());
PickFolder();
} catch (Exception e) {
Toast.makeText(ResultActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e(TAG, imageUri.toString(), e);
Log.i("fereshteh", imageUri.toString(), e);
}
} else {
Toast.makeText(ResultActivity.this, getString(R.string.toast_unexpected_error), Toast.LENGTH_SHORT).show();
}
}
}

private void copyFileToDownloads(Uri croppedFileUri) throws Exception {
String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
private String folderLocation;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
int FOLDERPICKER_CODE = 2;
if (requestCode == FOLDERPICKER_CODE && resultCode == Activity.RESULT_OK) {
folderLocation = intent.getExtras().getString("data");
try {
copyFileToDownloads(getIntent().getData(), folderLocation);
} catch (Exception e) {
Toast.makeText(ResultActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.i("fereshteh", "blah blah", e);
}
Log.i("folderLocation", folderLocation);

}
}
private void PickFolder(){
int FOLDERPICKER_CODE = 2;
Intent intent = new Intent(this, FolderPicker.class);
startActivityForResult(intent, FOLDERPICKER_CODE);
}

private void copyFileToDownloads(Uri croppedFileUri, String destination) throws Exception {
// String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String filename = String.format("%d_%s", Calendar.getInstance().getTimeInMillis(), croppedFileUri.getLastPathSegment());

File saveFile = new File(downloadsDirectoryPath, filename);
File saveFile = new File(destination, filename);

FileInputStream inStream = new FileInputStream(new File(croppedFileUri.getPath()));
FileOutputStream outStream = new FileOutputStream(saveFile);
Expand Down
49 changes: 47 additions & 2 deletions sample/src/main/java/com/yalantis/ucrop/sample/SampleActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.yalantis.ucrop.sample;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PorterDuff;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
Expand All @@ -25,12 +27,14 @@
import android.widget.TextView;
import android.widget.Toast;

import com.yalantis.ucrop.RatioSingleton;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;
import com.yalantis.ucrop.UCropFragment;
import com.yalantis.ucrop.UCropFragmentCallback;

import java.io.File;
import java.io.InputStream;
import java.util.Locale;
import java.util.Random;

Expand Down Expand Up @@ -81,14 +85,47 @@ public class SampleActivity extends BaseActivity implements UCropFragmentCallbac
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
Boolean back = (Boolean)getIntent().getSerializableExtra("back");
setupUI();

if (back != null){
startCrop(SavedUriSingleton.getUri());
}
}

// private void getDropboxIMGSize(Uri uri){
// BitmapFactory.Options options = new BitmapFactory.Options();
// options.inJustDecodeBounds = true;
// BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath());
// int imageHeight = options.outHeight;
// int imageWidth = options.outWidth;
// Toast.makeText(SampleActivity.this, String.valueOf(imageWidth), Toast.LENGTH_LONG).show();
//
// }

public void getImageSize(Uri uri){
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream input = this.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(input, null, options); input.close();
//Toast.makeText(SampleActivity.this, String.valueOf(options.outHeight), Toast.LENGTH_LONG).show();
// return new int[]{options.outWidth, options.outHeight};
RatioSingleton.setX(options.outWidth);
RatioSingleton.setY(options.outHeight);
}
catch (Exception e){}
// return new int[]{0,0};
}

@SuppressLint("MissingSuperCall")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == requestMode) {
final Uri selectedUri = data.getData();
SavedUriSingleton.setUri(selectedUri);
getImageSize(selectedUri);
if (selectedUri != null) {
startCrop(selectedUri);
} else {
Expand Down Expand Up @@ -140,7 +177,7 @@ public void afterTextChanged(Editable s) {
};

@SuppressWarnings("ConstantConditions")
private void setupUI() {
public void setupUI() {
findViewById(R.id.button_crop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -296,7 +333,13 @@ private UCrop basisConfig(@NonNull UCrop uCrop) {
*/
private UCrop advancedConfig(@NonNull UCrop uCrop) {
UCrop.Options options = new UCrop.Options();

switch (mRadioGroupAspectRatio.getCheckedRadioButtonId()) {
case R.id.radio_origin:
options.setRatioVisibility(true);
break;
default:
break;
}
switch (mRadioGroupCompressionSettings.getCheckedRadioButtonId()) {
case R.id.radio_png:
options.setCompressionFormat(Bitmap.CompressFormat.PNG);
Expand All @@ -306,11 +349,13 @@ private UCrop advancedConfig(@NonNull UCrop uCrop) {
options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
break;
}
options.setImageX(RatioSingleton.getX());
options.setCompressionQuality(mSeekBarQuality.getProgress());

options.setHideBottomControls(mCheckBoxHideBottomControls.isChecked());
options.setFreeStyleCropEnabled(mCheckBoxFreeStyleCrop.isChecked());


/*
If you want to configure how gestures work for all UCropActivity tabs

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yalantis.ucrop.sample;

import android.net.Uri;

public class SavedUriSingleton {
private static Uri uri;

public static void setUri(Uri uri) {
SavedUriSingleton.uri = uri;
}

public static Uri getUri() {
return uri;
}
}
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/activity_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:background="?colorAccent"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/format_crop_result_d_d"
app:titleTextColor="@android:color/white"/>
app:titleTextColor="@android:color/white" />

<com.yalantis.ucrop.view.UCropView
android:id="@+id/ucrop"
Expand Down
1 change: 1 addition & 0 deletions ucrop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ dependencies {
implementation "androidx.exifinterface:exifinterface:${androidx_exifinterface_version}"
implementation "androidx.transition:transition:${androidx_transition_version}"
implementation "com.squareup.okhttp3:okhttp:3.12.1"
compile 'lib.kashif:folderpicker:2.4'
}
23 changes: 23 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/RatioSingleton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.yalantis.ucrop;

import android.net.Uri;

public class RatioSingleton {
private static int x, y;

public static int getX() {
return x;
}

public static void setX(int x) {
RatioSingleton.x = x;
}

public static int getY() {
return y;
}

public static void setY(int y) {
RatioSingleton.y = y;
}
}
29 changes: 25 additions & 4 deletions ucrop/src/main/java/com/yalantis/ucrop/UCrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;

import com.yalantis.ucrop.model.AspectRatio;

Expand All @@ -34,6 +36,7 @@ public class UCrop {
public static final int REQUEST_CROP = 69;
public static final int RESULT_ERROR = 96;
public static final int MIN_SIZE = 10;
public static final int MAX_SIZE = 50;

private static final String EXTRA_PREFIX = BuildConfig.APPLICATION_ID;

Expand Down Expand Up @@ -102,12 +105,19 @@ public UCrop useSourceImageAspectRatio() {
* @param height max cropped image height
*/
public UCrop withMaxResultSize(@IntRange(from = MIN_SIZE) int width, @IntRange(from = MIN_SIZE) int height) {
if (width < MIN_SIZE) {
width = MIN_SIZE;
// if (width < MIN_SIZE) {
// width = MIN_SIZE;
// }
//
// if (height < MIN_SIZE) {
// height = MIN_SIZE;
// }
if (width > MAX_SIZE) {
width = MAX_SIZE;
}

if (height < MIN_SIZE) {
height = MIN_SIZE;
if (height > MAX_SIZE) {
height = MAX_SIZE;
}

mCropOptionsBundle.putInt(EXTRA_MAX_SIZE_X, width);
Expand Down Expand Up @@ -261,6 +271,8 @@ public static class Options {

public static final String EXTRA_COMPRESSION_FORMAT_NAME = EXTRA_PREFIX + ".CompressionFormatName";
public static final String EXTRA_COMPRESSION_QUALITY = EXTRA_PREFIX + ".CompressionQuality";
public static final String EXTRA_RATIO_VIS = EXTRA_PREFIX + ".RatioVis";
public static final String EXTRA_IMAGE_X = EXTRA_PREFIX + ".ImageX";

public static final String EXTRA_ALLOWED_GESTURES = EXTRA_PREFIX + ".AllowedGestures";

Expand Down Expand Up @@ -327,6 +339,11 @@ public void setCompressionQuality(@IntRange(from = 0) int compressQuality) {
mOptionBundle.putInt(EXTRA_COMPRESSION_QUALITY, compressQuality);
}


public void setRatioVisibility(boolean x) {
mOptionBundle.putBoolean(EXTRA_RATIO_VIS, x);
}

/**
* Choose what set of gestures will be enabled on each tab - if any.
*/
Expand Down Expand Up @@ -447,6 +464,10 @@ public void setToolbarColor(@ColorInt int color) {
mOptionBundle.putInt(EXTRA_TOOL_BAR_COLOR, color);
}

public void setImageX(int x) {
mOptionBundle.putInt(EXTRA_IMAGE_X, x);
}

/**
* @param color - desired resolved color of the statusbar
*/
Expand Down
10 changes: 9 additions & 1 deletion ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -24,6 +25,7 @@
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yalantis.ucrop.callback.BitmapCropCallback;
import com.yalantis.ucrop.model.AspectRatio;
Expand Down Expand Up @@ -261,6 +263,9 @@ private void processOptions(@NonNull Intent intent) {

int aspectRationSelectedByDefault = intent.getIntExtra(UCrop.Options.EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT, 0);
ArrayList<AspectRatio> aspectRatioList = intent.getParcelableArrayListExtra(UCrop.Options.EXTRA_ASPECT_RATIO_OPTIONS);
if (intent.getBooleanExtra(UCrop.Options.EXTRA_RATIO_VIS, false)) {
mWrapperStateAspectRatio.setVisibility(View.GONE);
}

if (aspectRatioX > 0 && aspectRatioY > 0) {
if (mWrapperStateAspectRatio != null) {
Expand Down Expand Up @@ -445,7 +450,10 @@ private void setupAspectRatioWidget(@NonNull Intent intent) {
aspectRatioList.add(new AspectRatio(getString(R.string.ucrop_label_original).toUpperCase(),
CropImageView.SOURCE_IMAGE_ASPECT_RATIO, CropImageView.SOURCE_IMAGE_ASPECT_RATIO));
aspectRatioList.add(new AspectRatio(null, 3, 2));
aspectRatioList.add(new AspectRatio(null, 16, 9));
//Toast.makeText(this, mGestureCropImageView.toString(), Toast.LENGTH_LONG).show();
if (intent.getIntExtra(UCrop.Options.EXTRA_IMAGE_X, 0) < 220) {
aspectRatioList.add(new AspectRatio(null, 16, 9));
}
}

LinearLayout wrapperAspectRatioList = findViewById(R.id.layout_aspect_ratio);
Expand Down
Loading