Commit f405ad01 by gao.chao

图片裁剪防止后台销毁,数据丢失

parent 0d006e94
......@@ -23,14 +23,15 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="d78a6d571c700f3ad33f374d81fed6a2" />
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......@@ -54,7 +55,8 @@
<activity android:name=".FragmentInteractiveActivity2" />
<activity android:name=".PageActivity" />
<activity android:name=".MyScanActivity" />
<activity android:name=".CutPicturesActivity" />
<activity android:name=".CutPicturesActivity"
android:screenOrientation="portrait"/>
</application>
<!-- 配置APP ID -->
......
package com.mayi.demo;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.util.BitmapUtil;
import com.mayi.fastdevelop.util.DrawableUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -91,76 +80,5 @@ public class MainActivity extends BaseActivity {
}
}
private Uri uri;
private File cameraSavePath;
//激活相机操作
private void goCamera() {
cameraSavePath = new File(Environment.getExternalStorageDirectory().getPath() + "/" + getPackageName() + "/" + System.currentTimeMillis() + ".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".uri", cameraSavePath);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(cameraSavePath);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
MainActivity.this.startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".uri", cameraSavePath);
} else {
uri = Uri.fromFile(cameraSavePath);
}
photoClip(uri);
} else if (requestCode == 2 && resultCode == RESULT_OK) {
// photoPath = getPhotoFromPhotoAlbum.getRealPathFromUri(this, data.getData());
Log.d("相册返回图片路径:", data.getData() + "");
photoClip(data.getData());
// Glide.with(MainActivity.this).load(photoPath).into(ivTest);
} else if (requestCode == 3 && resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
if (bundle != null) {
//在这里获得了剪裁后的Bitmap对象,可以用于上传
Bitmap image = bundle.getParcelable("data");
BitmapUtil.saveBitmapToFormat(image, Environment.getExternalStorageDirectory().getPath() + "/Download/11111.jpg");
// //设置到ImageView上
// ivTest.setImageBitmap(image);
// //也可以进行一些保存、压缩等操作后上传
// String path = saveImage("头像", image);
// Log.d("裁剪路径:", path);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void photoClip(Uri uri) {
// 调用系统中自带的图片剪裁
Intent intent1 = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
/*设置调用裁剪工具的action*/
intent1.setAction("com.android.camera.action.CROP");
/*创建一个指向需要操作文件(filename)的文件流。(可解决无法“加载问题”)*/
intent1.setDataAndType(uri, "image/*");
intent1.putExtra("crop", "true");
intent1.putExtra("scale", true);
intent1.putExtra("return-data", false);
Uri uritempFile = Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath() + "/" + System.currentTimeMillis() + ".jpg");
intent1.putExtra(MediaStore.EXTRA_OUTPUT, uritempFile);
intent1.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent1.putExtra("noFaceDetection", true);
MainActivity.this.startActivityForResult(intent1, 3);
}
}
......@@ -21,22 +21,28 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
private final int REQUESTCODE_CAMERA = 100;//拍照回调
private final int REQUESTCODE_CUT_PICTURES = 101;//裁剪图片回调
private String imgPath;//裁剪保存图片地址
private Uri uri;
private File cameraSavePath;//拍照保存图片地址
private String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
@Override
protected void onSaveInstanceState(Bundle outState) {
if (outState!=null){
outState.putString("imgPath",imgPath);
outState.putSerializable("cameraSavePath",cameraSavePath);
}
super.onSaveInstanceState(outState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState!=null){
imgPath=savedInstanceState.getString("imgPath");
cameraSavePath= (File) savedInstanceState.getSerializable("cameraSavePath");
}
PermissionUtil.requestPermission(new PermissionUtil.PermissionCallback() {
@Override
public void onSuccess(List<String> data) {
}
@Override
......@@ -59,6 +65,7 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
protected void goCamera() {
cameraSavePath = new File(Environment.getExternalStorageDirectory().getPath() + "/" + getPackageName() + "/" + System.currentTimeMillis() + ".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri=null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, getPackageName() + ".uri", cameraSavePath);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
......@@ -72,6 +79,7 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUESTCODE_CAMERA && resultCode == RESULT_OK) {
Uri uri=null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, getPackageName() + ".uri", cameraSavePath);
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment