关闭

imageView图片保存到本地和相册

标签: imageview图片
436人阅读 评论(0) 收藏 举报
分类:
package com.example.kk.myapplication;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.content.ClipboardManager;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
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 focusUsActivity extends Activity {

    private ImageView back;
    private TextView copyWeChatNumber;
    private ImageView two_dimension_code;
    private ClipboardManager cmb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.focus_us);

    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }

    private void init(){
        back=(ImageView)findViewById(R.id.focususBack);
        copyWeChatNumber=(TextView)findViewById(R.id.copyWeChatNumber);
        two_dimension_code=(ImageView)findViewById(R.id.two_dimension_code);
        cmb = (ClipboardManager)this.getSystemService(Context.CLIPBOARD_SERVICE);
        setComponent();
    }

    private void setComponent(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(focusUsActivity.this,localPhoneActivity.class);
                startActivity(intent);

            }
        });
        if(cmb.getText().toString().equals("ivali-mobi")){
            copyWeChatNumber.setBackgroundColor(Color.parseColor("#ADADAD"));
            copyWeChatNumber.setText("微信号已经复制到剪贴板");
        }else {
            copyWeChatNumber.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    copyWeChatNumber.setBackgroundColor(Color.parseColor("#ADADAD"));
                    copyWeChatNumber.setText("微信号已经复制到剪贴板");
                    cmb.setText("ivali-mobi");
                }
            });
        }

        two_dimension_code.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
               /* saveImage("ivali-icon.JPEG");*/ //保存到本地  /storage/emulated/0/saving_picture
                SaveImageToSysAlbum();  //保存到相册

                return false;
            }
        });


    }

    public static String getSDPath()
    {
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if(hasSDCard)
        {
            return Environment.getExternalStorageDirectory().toString()+"/saving_picture";

        }
        else{
            return "/data/data/com.example.kk.myapplication/saving_picture";
        }

    }

    public static Bitmap convertViewToBitmap(View view)
    {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    public void saveImage(String strFileName)
    {
        Bitmap bitmap = convertViewToBitmap(two_dimension_code);
        String strPath = getSDPath();

        try
        {
            File destDir = new File(strPath);
            if (!destDir.exists())
            {
                Log.d("MagicMirror", "Dir not exist create it " + strPath);
                destDir.mkdirs();
                Log.d("MagicMirror", "Make dir success: " + strPath);
            }

            File imageFile = new File(strPath + "/" + strFileName);
            imageFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    private void SaveImageToSysAlbum() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Bitmap bitmap = convertViewToBitmap(two_dimension_code);
            if (bitmap != null) {
                try {
                    ContentResolver cr = getContentResolver();
                    String url = MediaStore.Images.Media.insertImage(cr, bitmap,
                            String.valueOf(System.currentTimeMillis()), "");
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this, "没有sdcard,保存失败", Toast.LENGTH_SHORT).show();
        }
    }
}
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8146次
    • 积分:226
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:11篇
    • 译文:4篇
    • 评论:0条
    文章分类