写图片到SD卡

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:id="@+id/ll_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f9f9f9"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:gravity="center"
                android:text="姓名:"
                android:textColor="@color/black"
                android:textSize="17sp" />

            <EditText
                android:id="@+id/et_name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@+id/tv_name"
                android:background="@drawable/editext_selector"
                android:gravity="left|center"
                android:hint="请输入姓名"
                android:inputType="text"
                android:maxLength="12"
                android:textColor="@color/black"
                android:textColorHint="@color/grey"
                android:textCursorDrawable="@drawable/text_cursor"
                android:textSize="17sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >

            <TextView
                android:id="@+id/tv_age"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:gravity="center"
                android:text="年龄:"
                android:textColor="@color/black"
                android:textSize="17sp" />

            <EditText
                android:id="@+id/et_age"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@+id/tv_age"
                android:background="@drawable/editext_selector"
                android:gravity="left|center"
                android:hint="请输入年龄"
                android:inputType="number"
                android:maxLength="2"
                android:textColor="@color/black"
                android:textColorHint="@color/grey"
                android:textCursorDrawable="@drawable/text_cursor"
                android:textSize="17sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >

            <TextView
                android:id="@+id/tv_height"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:gravity="center"
                android:text="身高:"
                android:textColor="@color/black"
                android:textSize="17sp" />

            <EditText
                android:id="@+id/et_height"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@+id/tv_height"
                android:background="@drawable/editext_selector"
                android:gravity="left|center"
                android:hint="请输入身高"
                android:inputType="number"
                android:maxLength="3"
                android:textColor="@color/black"
                android:textColorHint="@color/grey"
                android:textCursorDrawable="@drawable/text_cursor"
                android:textSize="17sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >

            <TextView
                android:id="@+id/tv_weight"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:gravity="center"
                android:text="体重:"
                android:textColor="@color/black"
                android:textSize="17sp" />

            <EditText
                android:id="@+id/et_weight"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@+id/tv_weight"
                android:background="@drawable/editext_selector"
                android:gravity="left|center"
                android:hint="请输入体重"
                android:inputType="numberDecimal"
                android:maxLength="5"
                android:textColor="@color/black"
                android:textColorHint="@color/grey"
                android:textCursorDrawable="@drawable/text_cursor"
                android:textSize="17sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >

            <TextView
                android:id="@+id/tv_married"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:gravity="center"
                android:text="婚否:"
                android:textColor="@color/black"
                android:textSize="17sp" />

            <Spinner
                android:id="@+id/sp_married"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/tv_married"
                android:gravity="left|center"
                android:spinnerMode="dialog" />
        </RelativeLayout>
    </LinearLayout>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存图片到SD卡"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_path"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>
package com.example.forth;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout ll_info;
    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private boolean bMarried = false;

    private String mPath;
    private TextView tv_path;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_write);
        ll_info = findViewById(R.id.ll_info);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        tv_path = findViewById(R.id.tv_path);
        findViewById(R.id.btn_save).setOnClickListener(this);
        // 获取当前App的私有存储目录
        mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
        initTypeSpinner();
    }
    // 初始化婚姻状况的下拉框
    private void initTypeSpinner() {
        ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
                R.layout.item_select, typeArray);
        typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
        Spinner sp_married = findViewById(R.id.sp_married);
        sp_married.setPrompt("请选择婚姻状况");
        sp_married.setAdapter(typeAdapter);
        sp_married.setSelection(0);
        sp_married.setOnItemSelectedListener(new TypeSelectedListener());
    }

    private String[] typeArray = {"未婚", "已婚"};
    class TypeSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            bMarried = (arg2 == 0) ? false : true;
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_save) {
            String name = et_name.getText().toString();
            String age = et_age.getText().toString();
            String height = et_height.getText().toString();
            String weight = et_weight.getText().toString();
            if (TextUtils.isEmpty(name)) {
                showToast("请先填写姓名");
                return;
            } else if (TextUtils.isEmpty(age)) {
                showToast("请先填写年龄");
                return;
            } else if (TextUtils.isEmpty(height)) {
                showToast("请先填写身高");
                return;
            } else if (TextUtils.isEmpty(weight)) {
                showToast("请先填写体重");
                return;
            }

            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                // 获取线性布局ll_info绘图缓存中的位图数据
                Bitmap bitmap = ll_info.getDrawingCache();
                String file_path = mPath + DateUtil.getNowDateTime("") + ".png";
                // 把位图数据保存为图片文件
                FileUtil.saveImage(file_path, bitmap);
                // 回收位图对象
                bitmap.recycle();
                tv_path.setText("用户注册信息图片的保存路径为:\n" + file_path);
                showToast("图片已存入SD卡文件");
            } else {
                showToast("未发现已挂载的SD卡,请检查");
            }
        }
    }
    protected void onStart() {
        super.onStart();
        // 开启线性布局ll_info的绘图缓存
        ll_info.setDrawingCacheEnabled(true);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 关闭线性布局ll_info的绘图缓存
        ll_info.setDrawingCacheEnabled(false);
    }

    private void showToast(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值