Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fastdevelop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
高超
fastdevelop
Commits
f405ad01
Commit
f405ad01
authored
Sep 12, 2019
by
gao.chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
图片裁剪防止后台销毁,数据丢失
parent
0d006e94
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
87 deletions
+15
-87
app/src/main/AndroidManifest.xml
+5
-3
app/src/main/java/com/mayi/demo/MainActivity.java
+0
-82
fastDevelop/src/main/java/com/mayi/fastdevelop/base/BaseCutPicturesActivity.java
+10
-2
No files found.
app/src/main/AndroidManifest.xml
View file @
f405ad01
...
@@ -23,14 +23,15 @@
...
@@ -23,14 +23,15 @@
android:label=
"@string/app_name"
android:label=
"@string/app_name"
android:roundIcon=
"@mipmap/ic_launcher_round"
android:roundIcon=
"@mipmap/ic_launcher_round"
android:supportsRtl=
"true"
android:supportsRtl=
"true"
android:theme=
"@style/
AppTheme
"
>
android:theme=
"@style/
Theme.AppCompat.Light.NoActionBar
"
>
<meta-data
<meta-data
android:name=
"com.amap.api.v2.apikey"
android:name=
"com.amap.api.v2.apikey"
android:value=
"d78a6d571c700f3ad33f374d81fed6a2"
/>
android:value=
"d78a6d571c700f3ad33f374d81fed6a2"
/>
<activity
android:name=
".MainActivity"
>
<activity
android:name=
".MainActivity"
android:screenOrientation=
"portrait"
>
<intent-filter>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
<action
android:name=
"android.intent.action.MAIN"
/>
...
@@ -54,7 +55,8 @@
...
@@ -54,7 +55,8 @@
<activity
android:name=
".FragmentInteractiveActivity2"
/>
<activity
android:name=
".FragmentInteractiveActivity2"
/>
<activity
android:name=
".PageActivity"
/>
<activity
android:name=
".PageActivity"
/>
<activity
android:name=
".MyScanActivity"
/>
<activity
android:name=
".MyScanActivity"
/>
<activity
android:name=
".CutPicturesActivity"
/>
<activity
android:name=
".CutPicturesActivity"
android:screenOrientation=
"portrait"
/>
</application>
</application>
<!-- 配置APP ID -->
<!-- 配置APP ID -->
...
...
app/src/main/java/com/mayi/demo/MainActivity.java
View file @
f405ad01
package
com
.
mayi
.
demo
;
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.Bundle
;
import
android.os.Environment
;
import
android.provider.MediaStore
;
import
android.support.annotation.NonNull
;
import
android.support.annotation.NonNull
;
import
android.support.v4.content.FileProvider
;
import
android.support.v7.widget.LinearLayoutManager
;
import
android.support.v7.widget.LinearLayoutManager
;
import
android.support.v7.widget.RecyclerView
;
import
android.support.v7.widget.RecyclerView
;
import
android.util.Log
;
import
android.view.View
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.view.ViewGroup
;
import
android.widget.Button
;
import
android.widget.Button
;
import
com.mayi.fastdevelop.base.BaseActivity
;
import
com.mayi.fastdevelop.base.BaseActivity
;
import
com.mayi.fastdevelop.util.BitmapUtil
;
import
com.mayi.fastdevelop.util.DrawableUtils
;
import
com.mayi.fastdevelop.util.DrawableUtils
;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
public
class
MainActivity
extends
BaseActivity
{
public
class
MainActivity
extends
BaseActivity
{
@Override
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
super
.
onCreate
(
savedInstanceState
);
...
@@ -91,76 +80,5 @@ public class MainActivity extends BaseActivity {
...
@@ -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
);
}
}
}
fastDevelop/src/main/java/com/mayi/fastdevelop/base/BaseCutPicturesActivity.java
View file @
f405ad01
...
@@ -21,22 +21,28 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
...
@@ -21,22 +21,28 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
private
final
int
REQUESTCODE_CAMERA
=
100
;
//拍照回调
private
final
int
REQUESTCODE_CAMERA
=
100
;
//拍照回调
private
final
int
REQUESTCODE_CUT_PICTURES
=
101
;
//裁剪图片回调
private
final
int
REQUESTCODE_CUT_PICTURES
=
101
;
//裁剪图片回调
private
String
imgPath
;
//裁剪保存图片地址
private
String
imgPath
;
//裁剪保存图片地址
private
Uri
uri
;
private
File
cameraSavePath
;
//拍照保存图片地址
private
File
cameraSavePath
;
//拍照保存图片地址
private
String
[]
permissions
=
{
Manifest
.
permission
.
CAMERA
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
};
private
String
[]
permissions
=
{
Manifest
.
permission
.
CAMERA
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
};
@Override
@Override
protected
void
onSaveInstanceState
(
Bundle
outState
)
{
protected
void
onSaveInstanceState
(
Bundle
outState
)
{
if
(
outState
!=
null
){
outState
.
putString
(
"imgPath"
,
imgPath
);
outState
.
putSerializable
(
"cameraSavePath"
,
cameraSavePath
);
}
super
.
onSaveInstanceState
(
outState
);
super
.
onSaveInstanceState
(
outState
);
}
}
@Override
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
super
.
onCreate
(
savedInstanceState
);
if
(
savedInstanceState
!=
null
){
imgPath
=
savedInstanceState
.
getString
(
"imgPath"
);
cameraSavePath
=
(
File
)
savedInstanceState
.
getSerializable
(
"cameraSavePath"
);
}
PermissionUtil
.
requestPermission
(
new
PermissionUtil
.
PermissionCallback
()
{
PermissionUtil
.
requestPermission
(
new
PermissionUtil
.
PermissionCallback
()
{
@Override
@Override
public
void
onSuccess
(
List
<
String
>
data
)
{
public
void
onSuccess
(
List
<
String
>
data
)
{
}
}
@Override
@Override
...
@@ -59,6 +65,7 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
...
@@ -59,6 +65,7 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
protected
void
goCamera
()
{
protected
void
goCamera
()
{
cameraSavePath
=
new
File
(
Environment
.
getExternalStorageDirectory
().
getPath
()
+
"/"
+
getPackageName
()
+
"/"
+
System
.
currentTimeMillis
()
+
".jpg"
);
cameraSavePath
=
new
File
(
Environment
.
getExternalStorageDirectory
().
getPath
()
+
"/"
+
getPackageName
()
+
"/"
+
System
.
currentTimeMillis
()
+
".jpg"
);
Intent
intent
=
new
Intent
(
MediaStore
.
ACTION_IMAGE_CAPTURE
);
Intent
intent
=
new
Intent
(
MediaStore
.
ACTION_IMAGE_CAPTURE
);
Uri
uri
=
null
;
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
N
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
N
)
{
uri
=
FileProvider
.
getUriForFile
(
this
,
getPackageName
()
+
".uri"
,
cameraSavePath
);
uri
=
FileProvider
.
getUriForFile
(
this
,
getPackageName
()
+
".uri"
,
cameraSavePath
);
intent
.
addFlags
(
Intent
.
FLAG_GRANT_READ_URI_PERMISSION
);
intent
.
addFlags
(
Intent
.
FLAG_GRANT_READ_URI_PERMISSION
);
...
@@ -72,6 +79,7 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
...
@@ -72,6 +79,7 @@ public abstract class BaseCutPicturesActivity extends BaseActivity {
@Override
@Override
protected
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
)
{
protected
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
)
{
if
(
requestCode
==
REQUESTCODE_CAMERA
&&
resultCode
==
RESULT_OK
)
{
if
(
requestCode
==
REQUESTCODE_CAMERA
&&
resultCode
==
RESULT_OK
)
{
Uri
uri
=
null
;
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
N
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
N
)
{
uri
=
FileProvider
.
getUriForFile
(
this
,
getPackageName
()
+
".uri"
,
cameraSavePath
);
uri
=
FileProvider
.
getUriForFile
(
this
,
getPackageName
()
+
".uri"
,
cameraSavePath
);
}
else
{
}
else
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment