基本思想:在Android上添加从相册中取图片和将图片保存到相册中 参考nihui大佬代码(真心不会Android)
一、创建按钮同时并拖动到指定位置,这里使用得constraintLayout布局
(1)、拖拽按钮
(2)、然后修改按钮得名字和添加imageview界面
(3)、在Mainactivity函数中进行java代码调用编写
(4)、详细代码片段-1
imageView = (ImageView) findViewById(R.id.imageView);
Button buttonImage = (Button) findViewById(R.id.button);
buttonImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i, 1);
}
});
代码片段-2
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
try
{
switch (requestCode)
{
case 1 :
bitmap = decodeUri(selectedImage);
yourSelectedImage = bitmap.copy(Bitmap.Config.ARGB_8888, true);
imageView.setImageBitmap(bitmap);
break;
default:
break;
}
}
catch (FileNotFoundException e)
{
Log.e("MainActivity", "FileNotFoundException");
return;
}
}
}
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 640;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
// Rotate according to EXIF
int rotate = 0;
try
{
ExifInterface exif = new ExifInterface(getContentResolver().openInputStream(selectedImage));
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
}
catch (IOException e)
{
Log.e("MainActivity", "ExifInterface IOException");
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
public void requestPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
// 申请 相机 麦克风权限
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
}
}
(5)、添加手机存储器得读写权限
(6)、至此就完成了手机相册得打开和图片得获取 展示
二、图片保存功能(场景:从相册获得相片进行了检测、画框之后,然后在保存到相册中)
(1)、拖拽按钮得方法、修改界面布局、修改按钮
(2)、对应得代码布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开相册" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存图片" />
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
(3)、修改对应的保存图片代码
(4)、代码片段-1
Button saveImage = (Button) findViewById(R.id.button2);
saveImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (bitmap == null){
Toast.makeText(MainActivity.this,"请选择需要检测的图片",Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName = "detected_" + format.format(new Date()) + ".jpg";
Bitmap bmp = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
saveBitmap(bmp,fileName);
Toast.makeText(MainActivity.this,"图片保存成功",Toast.LENGTH_LONG).show();
}
});
代码片段-2
public void saveBitmap(Bitmap bitmap, String bitName){
String fileName;
File file;
if(Build.BRAND .equals("Xiaomi") ){
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + bitName;
}else{
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + bitName;
}
file = new File(fileName);
if(file.exists()){
file.delete();
}
FileOutputStream out;
try{
out = new FileOutputStream(file);
if(bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)){
out.flush();
out.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), bitName, null);
}
} catch (IOException e){
e.printStackTrace();
}
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)));
}
(5)结果手机app