|
|
package net.micode.notes.ui;
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.content.ContentUris;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.database.Cursor;
|
|
|
import android.graphics.Bitmap;
|
|
|
import android.graphics.BitmapFactory;
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
import android.icu.util.Calendar;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
import android.provider.DocumentsContract;
|
|
|
import android.provider.MediaStore;
|
|
|
import android.view.View;
|
|
|
import android.widget.Button;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
import com.yalantis.ucrop.UCrop;
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.channels.FileChannel;
|
|
|
|
|
|
public class UCropPictureActivity extends AppCompatActivity {
|
|
|
|
|
|
Button btnTest;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_main);
|
|
|
btnTest = (Button) findViewById(R.id.btn_test);
|
|
|
/*
|
|
|
btnTest.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
|
try {
|
|
|
startCrop();
|
|
|
} catch (FileNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
});*/
|
|
|
try {
|
|
|
startCrop();
|
|
|
} catch (FileNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
finish();
|
|
|
}
|
|
|
|
|
|
//维护
|
|
|
public static String FileSaveToInside(Context context, String fileName, Bitmap bitmap) {
|
|
|
FileOutputStream fos = null;
|
|
|
String path = null;
|
|
|
try {
|
|
|
//设置路径 /Android/data/com.panyko.filesave/Pictures/
|
|
|
File folder = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
|
|
|
//判断目录是否存在
|
|
|
//目录不存在时自动创建
|
|
|
if (folder.exists() ||folder.mkdir()) {
|
|
|
File file = new File(folder, fileName);
|
|
|
fos = new FileOutputStream(file);
|
|
|
//写入文件
|
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
|
|
fos.flush();
|
|
|
path = file.getAbsolutePath();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
|
|
|
} finally {
|
|
|
try {
|
|
|
if (fos != null) {
|
|
|
//关闭流
|
|
|
fos.close();
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
//返回路径
|
|
|
return path;
|
|
|
}
|
|
|
|
|
|
private void startCrop() throws FileNotFoundException {
|
|
|
|
|
|
//Uri sourceUri = Uri.parse("http://star.xiziwang.net/uploads/allimg/140512/19_140512150412_1.jpg");
|
|
|
//裁剪后保存到文件中
|
|
|
Uri sourceUri = Uri.fromFile(new File(getCacheDir(), "123.jpeg"));
|
|
|
Uri destinationUri = Uri.fromFile(new File(getCacheDir(), "SampleCropImage.jpeg"));
|
|
|
UCrop.of(sourceUri, destinationUri).withAspectRatio(9, 16).withMaxResultSize(2000, 2500).start(this);
|
|
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(destinationUri));
|
|
|
|
|
|
|
|
|
String save=FileSaveToInside(this,"1234",bitmap);
|
|
|
|
|
|
SharedPreferences sharedPreferences=getSharedPreferences("data1", Context.MODE_WORLD_WRITEABLE);//xml
|
|
|
SharedPreferences.Editor editor=sharedPreferences.edit();
|
|
|
|
|
|
editor.putString("PERSONAL_URI",save);
|
|
|
|
|
|
editor.putInt("DEFAULT",0);
|
|
|
|
|
|
editor.commit();
|
|
|
finish();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
//裁切成功
|
|
|
if (requestCode == UCrop.REQUEST_CROP) {
|
|
|
Uri croppedFileUri = UCrop.getOutput(data);
|
|
|
//获取默认的下载目录
|
|
|
String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
|
|
|
@SuppressLint({"NewApi", "LocalSuppress"}) String filename = String.format("%d_%s", Calendar.getInstance().getTimeInMillis(), croppedFileUri.getLastPathSegment());
|
|
|
File saveFile = new File(downloadsDirectoryPath, filename);
|
|
|
//保存下载的图片
|
|
|
FileInputStream inStream = null;
|
|
|
FileOutputStream outStream = null;
|
|
|
FileChannel inChannel = null;
|
|
|
FileChannel outChannel = null;
|
|
|
try {
|
|
|
inStream = new FileInputStream(new File(croppedFileUri.getPath()));
|
|
|
outStream = new FileOutputStream(saveFile);
|
|
|
inChannel = inStream.getChannel();
|
|
|
outChannel = outStream.getChannel();
|
|
|
inChannel.transferTo(0, inChannel.size(), outChannel);
|
|
|
Toast.makeText(this, "裁切后的图片保存在:" + saveFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
outChannel.close();
|
|
|
outStream.close();
|
|
|
inChannel.close();
|
|
|
inStream.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//裁切失败
|
|
|
if (resultCode == UCrop.RESULT_ERROR) {
|
|
|
Toast.makeText(this, "裁切图片失败", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// 专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使
|
|
|
@SuppressLint("NewApi")
|
|
|
public static String getPathByUri4kitkat(final Context context, final Uri uri) {
|
|
|
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
|
|
// DocumentProvider
|
|
|
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
|
|
|
if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
|
|
|
final String docId = DocumentsContract.getDocumentId(uri);
|
|
|
final String[] split = docId.split(":");
|
|
|
final String type = split[0];
|
|
|
if ("primary".equalsIgnoreCase(type)) {
|
|
|
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
|
|
}
|
|
|
} else if (isDownloadsDocument(uri)) {// DownloadsProvider
|
|
|
final String id = DocumentsContract.getDocumentId(uri);
|
|
|
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
|
|
|
Long.valueOf(id));
|
|
|
return getDataColumn(context, contentUri, null, null);
|
|
|
} else if (isMediaDocument(uri)) {// MediaProvider
|
|
|
final String docId = DocumentsContract.getDocumentId(uri);
|
|
|
final String[] split = docId.split(":");
|
|
|
final String type = split[0];
|
|
|
Uri contentUri = null;
|
|
|
if ("image".equals(type)) {
|
|
|
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
|
|
} else if ("video".equals(type)) {
|
|
|
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
|
|
} else if ("audio".equals(type)) {
|
|
|
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
|
|
}
|
|
|
final String selection = "_id=?";
|
|
|
final String[] selectionArgs = new String[] { split[1] };
|
|
|
return getDataColumn(context, contentUri, selection, selectionArgs);
|
|
|
}
|
|
|
} else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore
|
|
|
// (and
|
|
|
// general)
|
|
|
return getDataColumn(context, uri, null, null);
|
|
|
} else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
|
|
|
return uri.getPath();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Get the value of the data column for this Uri. This is useful for
|
|
|
* MediaStore Uris, and other file-based ContentProviders.
|
|
|
*
|
|
|
* @param context
|
|
|
* The context.
|
|
|
* @param uri
|
|
|
* The Uri to query.
|
|
|
* @param selection
|
|
|
* (Optional) Filter used in the query.
|
|
|
* @param selectionArgs
|
|
|
* (Optional) Selection arguments used in the query.
|
|
|
* @return The value of the _data column, which is typically a file path.
|
|
|
*/
|
|
|
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
|
|
|
Cursor cursor = null;
|
|
|
final String column = "_data";
|
|
|
final String[] projection = { column };
|
|
|
try {
|
|
|
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
final int column_index = cursor.getColumnIndexOrThrow(column);
|
|
|
return cursor.getString(column_index);
|
|
|
}
|
|
|
} finally {
|
|
|
if (cursor != null)
|
|
|
cursor.close();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param uri
|
|
|
* The Uri to check.
|
|
|
* @return Whether the Uri authority is ExternalStorageProvider.
|
|
|
*/
|
|
|
public static boolean isExternalStorageDocument(Uri uri) {
|
|
|
return "com.android.externalstorage.documents".equals(uri.getAuthority());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param uri
|
|
|
* The Uri to check.
|
|
|
* @return Whether the Uri authority is DownloadsProvider.
|
|
|
*/
|
|
|
public static boolean isDownloadsDocument(Uri uri) {
|
|
|
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param uri
|
|
|
* The Uri to check.
|
|
|
* @return Whether the Uri authority is MediaProvider.
|
|
|
*/
|
|
|
public static boolean isMediaDocument(Uri uri) {
|
|
|
return "com.android.providers.media.documents".equals(uri.getAuthority());
|
|
|
}
|
|
|
|
|
|
}
|