imageView图片保存到本地和相册

转载 2015年11月20日 15:26:32
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();
        }
    }
}

Android调用系统相册和相机选择图片并显示在imageview中

Android调用系统相册和相机选择图片并显示在imageview中,在系统调用相机拍摄中,直接返回的是经过压缩处理后的图像,当你直接把返还后的图片放在imageview中时 图片就会非常的模糊,所以...

从相机或者相册,获取图片显示在ImageView上

import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; ...

android 使用本机相机照相或用本地相册更新imageview里面的图片

1.点击图片弹出popupwindow对话框 private void showAvatarPop() {         View view = LayoutInflater.fro...

Android打开本地相册选取图片并显示在ImageView上

这篇文章主要是为了方便以后本人查阅

Android4.4以后从相册中获取图片显示到Imageview上实现

4.4之后的版本从相册中获取图片显示到Imageview上的实现安卓4.4以后,从相册中获取图片返回的Uri发生了改变,所以要针对不同的情况进行适配了。...

viewPager+photoView实现图片轮播和手势缩放功能 支持手势缩放的imageView 如何实现相册左右滑动和手势缩放 如何让图片自适应控件大小 photoView如何使用(上)

photoView+ViewPager实现相册左右滑动和手势缩放,当前页放大后,滑动到下一页,前面放大的页面恢复默认大小状态...

Android开发——相册拍照_02.将拍照得到或相册中选择的图片显示在ImageView中

继续之前所讲 同时打开相册或拍照完成后,就可以选择图片或拍照,然后目的是将所得图片显示在ImageView控件中之前写的工具类里调用了startActivityForResult方法,于是在需要重写...

调相册绑定到ImageView上(并根据imageView的大小剪切图片)

public class MainActivity extends Activity { private ImageView img; private View.OnClickList...

Android ImageView 显示网络图片并下载保存

import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; i...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:imageView图片保存到本地和相册
举报原因:
原因补充:

(最多只允许输入30个字)