Android模拟上传头像

直接上代码
xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:id="@+id/imageView1"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="切换图片"
        android:onClick="onClickChange"
        />



</LinearLayout>

java文件

package wnl.myndk;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.Calendar;


public class Demo2 extends Activity {


    private ImageView imageView;
    private File tempFile;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo2);
        imageView = (ImageView) findViewById(R.id.imageView1);  //初始化ImageView控件
        sp = getSharedPreferences("HeaderImage", MODE_PRIVATE);
        String path = sp.getString("tempFile", "");
        if (!"".equals(path)) {
            imageView.setImageDrawable(Drawable.createFromPath(path));
        }


    }

    public void onClickChange(View view) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.putExtra("crop", "true");// 出现剪辑的小方框,不然没有剪辑功能,只能选取图片
        //宽高比
        intent.putExtra("aspectX", 1); // 出现放大和缩小
        intent.putExtra("aspectY", 1); // 出现放大和缩小
        intent.setType("image/*"); // 查看类型 详细的类型在 com.google.android.mms.ContentType

        String path = sp.getString("tempFile", "");
        if (!"".equals(path))
            tempFile = new File(path);
        else {
            tempFile = new File("/sdcard/headerImage" + ".jpg");
            sp.edit().putString("tempFile", tempFile.getAbsolutePath()).commit();
        }


        File temp = new File("/sdcard/headerImage/");//自已项目 文件夹
        if (!temp.exists()) {
            temp.mkdir();
        }
        intent.putExtra("output", Uri.fromFile(tempFile));  // 传入目标文件
        intent.putExtra("outputFormat", "JPEG"); //输入文件格式

        Intent wrapperIntent = Intent.createChooser(intent, "选择头像图片"); //开始 并设置标题
        startActivityForResult(wrapperIntent, 1); // 设返回 码为 1  onActivityResult 中的 requestCode 对应
    }

    //调用成功反回方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                imageView.setImageDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));
                Toast.makeText(Demo2.this, tempFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值