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

适配android R #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -10,12 +10,15 @@
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import java.io.File;
import java.io.FileNotFoundException;

import androidx.core.content.FileProvider;
import java.io.IOException;
import java.io.OutputStream;


/**
Expand Down Expand Up @@ -188,9 +191,14 @@ public static Intent crop(Activity activity, Uri uri, int w, int h, int aspectX,
}

/*解决小米miui系统调用系统裁剪图片功能camera.action.CROP后崩溃或重新打开app的问题*/
String pathName = new StringBuffer().append("file:///").append(FileUtils.getImageCacheDir(activity)).append(File.separator)
.append(System.currentTimeMillis()).append(".jpg").toString();
cropPictureTempUri = Uri.parse(pathName);
String filename = File.separator + System.currentTimeMillis() + ".jpg";
File outputFile = new File(Build.VERSION.SDK_INT >= 30 ? FileUtils.getExtPicturesPath().getPath() : FileUtils.getImageCacheDir(activity), filename);
Uri outputPictureUri = Uri.parse("file:///" + outputFile);
if(!checkOutputPictureUri(activity, outputPictureUri)){
Log.e("PictureSelectUtils", "#### 读取输出图片URI失败 ####");
}

cropPictureTempUri = outputPictureUri;
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropPictureTempUri);//输出路径(裁剪后的保存路径)
// 输出格式
intent.putExtra("outputFormat", "JPEG");
Expand All @@ -214,4 +222,29 @@ public static Bitmap dealCrop(Context context) {
return bitmap;
}

/**
* 检查输出图片的uri是否可以能正常打开
* @param context
* @param uri
* @return
*/
private static boolean checkOutputPictureUri(Context context, Uri uri){
OutputStream openOutputStream = null;
try {
openOutputStream = context.getContentResolver().openOutputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} finally {
if(openOutputStream != null){
try {
openOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}

}