Android:点击按钮,保存图片到本地相册
【摘要】
效果:点击按钮,将ImageView的图片保存到相册中。
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearL...
效果:点击按钮,将ImageView的图片保存到相册中。
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:id="@+id/tvSave"
android:layout_marginTop="15dp"
android:text="保存"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:id="@+id/tvpath"
android:layout_marginTop="15dp"
android:text="保存"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:id="@+id/tvpath2"
android:layout_marginTop="15dp"
android:text="保存"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imgPingjia2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="@mipmap/img_default_bg" />
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
MainActivity.java
package com.netease.nim.imagesave;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button tvSave;
ImageView imageShowPic;
TextView tvpath;
TextView tvpath2;
private String savedPath = Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + String.valueOf(System.currentTimeMillis()) + ".png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvpath = findViewById(R.id.tvpath);
tvpath2 = findViewById(R.id.tvpath2);
imageShowPic = findViewById(R.id.imgPingjia2);
imageShowPic.setImageResource( R.mipmap.demo_tribe_header );
tvSave = findViewById(R.id.tvSave);
tvSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// tvSave.setClickable(false);
//相关权限的申请 存储权限
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// 申请一个(或多个)权限,并提供用于回调返回的获取码(用户定义)
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
// mLDialog.setDialogText("正在保存图片...");
// mLDialog.show();
saveMyBitmap("AuthCode", createViewBitmap(imageShowPic));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//权限申请的回调
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// mLDialog.setDialogText("正在保存图片...");
// mLDialog.show();
try {
saveMyBitmap("AuthCode", createViewBitmap(imageShowPic));
} catch (Exception e) {
e.printStackTrace();
}
} else {
tvSave.setClickable(true);
Toast.makeText(MainActivity.this, "请先开启读写权限", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
//使用IO流将bitmap对象存到本地指定文件夹
public void saveMyBitmap(final String bitName, final Bitmap bitmap) {
new Thread(new Runnable() {
@Override
public void run() {
String filePath = Environment.getExternalStorageDirectory().getPath();
File file = new File(savedPath);
try {
file.createNewFile();
FileOutputStream fOut = null;
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
Message msg = Message.obtain();
msg.obj = file.getPath();
handler.sendMessage(msg);
//Toast.makeText(PayCodeActivity.this, "保存成功", Toast.LENGTH_LONG).show();
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String picFile = (String) msg.obj;
String[] split = picFile.split("/");
String fileName = split[split.length - 1];
try {
MediaStore.Images.Media.insertImage(getApplicationContext()
.getContentResolver(), picFile, fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"
+ picFile)));
Toast.makeText(MainActivity.this, "图片保存图库成功", Toast.LENGTH_LONG).show();
// if (mLDialog != null && mLDialog.isShowing()) {
// mLDialog.dismiss();
// }
tvSave.setClickable(true);
}
};
//将要存为图片的view传进来 生成bitmap对象
public Bitmap createViewBitmap(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
参考资源:
https://download.csdn.net/download/wangqing830414/11197239
文章来源: zstar.blog.csdn.net,作者:zstar-_,版权归原作者所有,如需转载,请联系作者。
原文链接:zstar.blog.csdn.net/article/details/120393529
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)