布局文件
赞
踩
权限管理
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".activity.TextActivity">
-
- <RelativeLayout
- android:layout_centerVertical="true"
- android:id="@+id/view"
- android:layout_width="match_parent"
- android:layout_height="500dp"
- android:background="#FF3600"
- >
-
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="match_parent"
- android:layout_height="300dp"
- />
-
- <TextView
- android:textSize="16dp"
- android:layout_marginBottom="5dp"
- android:layout_centerHorizontal="true"
- android:layout_alignParentBottom="true"
- android:text="世间万物中,表里如一者,又有几何."
- android:textColor="#FFFFFF"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
-
- </RelativeLayout>
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:text="点击!!!"
- />
-
- </RelativeLayout>
Activity文件
- package com.zhongqin.zhangshuai.activity;
-
- import android.Manifest;
- import android.annotation.TargetApi;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.provider.MediaStore;
- import android.util.Log;
- import android.view.View;
- import android.widget.ImageView;
-
- import com.bumptech.glide.Glide;
- import com.zhongqin.zhangshuai.R;
-
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
-
- public class TextActivity extends BaseActivity {
-
- private 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);
-
- // 最后通知图库更新
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + picFile)));
- toastMessage("图片保存图库成功");
-
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- };
- private View view;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_text);
-
- view = findViewById(R.id.view);
- Glide.with(this).load("https://kedou-1300812733.cos.ap-beijing.myqcloud.com/admin/2020/02/25/b51d6098accc42828f482c0c6cfe9532.png").into((ImageView)findViewById(R.id.imageView));
-
- findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //必须View渲染完毕处理
- view.getHandler().post(new Runnable() {
- @Override
- public void run() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- checkAndRequestPermission(); //权限申请
- } else {
- //小于6.0,不用申请权限,直接执行
- saveMyBitmap("aaa",createViewBitmap(view));
- }
- }
- });
- }
- });
- }
-
- @TargetApi(Build.VERSION_CODES.M)
- private void checkAndRequestPermission() {
- List<String> lackedPermission = new ArrayList<String>();
-
- if (!(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
- lackedPermission.add(Manifest.permission.READ_EXTERNAL_STORAGE);
- }
-
- if (!(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
- lackedPermission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
- }
-
- // 权限都已经有了,那么直接调用SDK
- if (lackedPermission.size() == 0) {
- saveMyBitmap("aaa",createViewBitmap(view));
- } else {
- // 请求所缺少的权限,在onRequestPermissionsResult中再看是否获得权限,如果获得权限就可以调用SDK,否则不要调用SDK。
- String[] requestPermissions = new String[lackedPermission.size()];
- lackedPermission.toArray(requestPermissions);
- requestPermissions(requestPermissions, 1024);
- }
- }
-
- private boolean hasAllPermissionsGranted(int[] grantResults) {
- for (int grantResult : grantResults) {
- if (grantResult == PackageManager.PERMISSION_DENIED) {
- return false;
- }
- }
- return true;
- }
-
-
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == 1024 && hasAllPermissionsGranted(grantResults)) {
- saveMyBitmap("aaa",createViewBitmap(view));
- } else {
- toastMessage("缺少权限");
- }
- }
-
- 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(filePath + "/DCIM/Camera/" + bitName + ".png");
- 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);
- Log.e("TAGTAGTAG","over:"+file.getPath());
- fOut.flush();
- fOut.close();
- } catch (IOException e) {
- Log.e("TAGTAGTAG","!!!:"+e.getMessage());
- e.printStackTrace();
- }
- }
- }).start();
- }
-
- //创建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;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。