关闭

Android将View保存为图片到SD卡

367人阅读 评论(0) 收藏 举报
分类:

第一步:

申请权限

<!-- 往SDCard写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
第二步:

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/mainColor"
        android:gravity="center"
        android:text="自动详情图"
        android:textColor="#fff"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/etContent"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:hint="输入文字"
        android:textColorHint="#C1C1C1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:onClick="Button"
        android:text="Button" />

    <RelativeLayout
        android:id="@+id/rlContent"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginTop="10dp"
        android:background="#D73ADC"
        android:gravity="center">

        <TextView
            android:id="@+id/tvContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文字"
            android:textColor="#fff"
            android:textSize="18sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/ivPreview"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginTop="10dp"
        android:background="#C0C0C0"
        android:gravity="center" />

</LinearLayout>
第三步:

Java代码

package com.xiong.autodetail.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xiong.autodetail.R;
import com.xiong.autodetail.utils.FileUtil;
import com.xiong.autodetail.utils.ToastUtil;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends BaseActivity {

    private EditText etContent;
    private TextView tvContent;
    private RelativeLayout rlContent;
    private ImageView ivPreview;
    private Context context = this;
    final private static int REFRESH_COMPLETE = 0;//Message标记
    final private static int SET_TEXT_TIME = 1;//设置文本延时时间(单位毫秒),避免图片上文字生成不完整

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }

    //初始化控件
    private void initView() {
        etContent = (EditText) findViewById(R.id.etContent);
        tvContent = (TextView) findViewById(R.id.tvContent);
        rlContent = (RelativeLayout) findViewById(R.id.rlContent);
        ivPreview = (ImageView)findViewById(R.id.ivPreview);
    }

    //按钮响应事件
    public void Button(View view) {
        if(etContent.length() > 0){
            String content = etContent.getText().toString();
            tvContent.setText(content);
            mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, SET_TEXT_TIME);
        }else{
            ToastUtil.show("未输入!");
        }
    }

    //Hadle
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_COMPLETE:
                    save(getBitmap(rlContent));
                    etContent.setText("");
                    break;
            }
        }
    };

    //将View转为Bitmap
    public Bitmap getBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }

    //将Bitmap保存为图片
    private void save(Bitmap bitmap) {
        try {
            String fileName = tvContent.getText().toString().substring(0, 2) + ".png";
            FileOutputStream stream = new FileOutputStream(FileUtil.getDownPath() + fileName);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            stream.flush();
            stream.close();
            ToastUtil.show("操作完成");
            previewImg(FileUtil.getDownPath() + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //预览图片
    private void previewImg(String filePath){
        Picasso.with(context).load(new File(filePath)).into(ivPreview);
    }

}


工具类方法:

    public static String getDownPath() {
        String path = Environment.getExternalStorageDirectory() + "/000_AAA/";
        isDownFolder(path);
        return path;
    }

    //判断文件夹是否存在,如果不存在则创建文件夹
    public static void isDownFolder(String downFolder) {
        File file = new File(downFolder);
        if (!file.exists()) {
            file.mkdir();
        }
    }

Picasso是一个第三方框架,这里只是用来加载预览图的。

效果图:






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:87013次
    • 积分:1840
    • 等级:
    • 排名:千里之外
    • 原创:108篇
    • 转载:1篇
    • 译文:0篇
    • 评论:33条
    最新评论