zoukankan      html  css  js  c++  java
  • 图记 2016.1.7 获取本地图片、Bitmap转image

                  这几天完成的内容有:

                  1.“添加图片”按钮

                  2.添加图片功能

                  遇到的问题:

                  我想要将添加图片按钮放在右下角,所以采用了相对布局,但是问题随之二来,因为将导航栏设置成了半透明,所以图片放到右下角之后,半透明的导航栏会把图片挡住,最后采取的办法是把图片高度变大,导航栏挡住多处的部分,也算是勉强实现要求了,但是对于一些没有虚拟导航栏的手机,按钮距离底部的间隔会显得略大,但是目前技术实在想不出更好的办法了

      

    打开图片

      在Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量,然后设置类型为“image/*”,就可获得android手机内的所有image。然后再通过startActivityForResult让我们选择的image返回到Activity中去

      AddPicActivity.java

    package cn.lixyz.tuji;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;
    
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.database.sqlite.SQLiteDatabase;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.Toast;
    import cn.lixyz.tuji.Util.GetDate;
    import cn.lixyz.tuji.sqlite.MySQLiteOpenHelper;
    
    /**
     * 添加图片类 选择图片之后,先判断用户是否安装SD卡,如果安装,则在SD卡中新建一个cn.lixyz.tuji目录,将选择的图片复制到目录中
     * 
     * @author LGB
     *
     */
    
    public class AddPicActivity extends Activity implements View.OnClickListener {
    
        private ImageView iv_select_img;
        private EditText et_image_name, et_image_tag, et_image_remark;
        private Button bt_submit;
        private Bitmap bitmap;
        private File sdPath;
        private String fileName = "TUJIE" + new Date().getTime();
        private FileOutputStream fos;
        private MySQLiteOpenHelper msoh;
        private SQLiteDatabase database;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_addpic);
    
            initView();
    
        }
    
        private void initView() {
            iv_select_img = (ImageView) findViewById(R.id.iv_select_img);
            et_image_name = (EditText) findViewById(R.id.et_image_name);
            et_image_tag = (EditText) findViewById(R.id.et_image_tag);
            et_image_remark = (EditText) findViewById(R.id.et_image_remark);
            bt_submit = (Button) findViewById(R.id.bt_submit);
            iv_select_img.setOnClickListener(this);
            bt_submit.setOnClickListener(this);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                ContentResolver cr = this.getContentResolver();
                try {
                    bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                    long i = new Date().getTime();
                    copyPicToSDCard(bitmap);
                    Log.d("TTTT", "共耗时:" + (new Date().getTime() - i));
                    iv_select_img.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    Log.e("Exception", e.getMessage(), e);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.iv_select_img:
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);
    
                break;
            case R.id.bt_submit:
                String picName = et_image_name.getText().toString().trim();
                String picTag = et_image_tag.getText().toString().trim();
                String picRemark = et_image_remark.getText().toString().trim();
                msoh = new MySQLiteOpenHelper(AddPicActivity.this, "TujiData", null, 1);
                database = msoh.getWritableDatabase();
                ContentValues cv = new ContentValues();
                cv.put("picName", picName);
                cv.put("picTag", picTag);
                cv.put("picRemark", picRemark);
                cv.put("fileName", fileName);
                cv.put("date", new GetDate().getDate());
                database.insert("pics", null, cv);
                database.close();
    
                break;
            }
        }
    
        private void copyPicToSDCard(Bitmap bm) throws IOException {
            if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
                /* 得到SD卡得路径 */
                sdPath = Environment.getExternalStorageDirectory();
                try {
                    File dir = new File(sdPath + "/cn.lixyz.TuJi");
    
                    fos = new FileOutputStream(new File(dir + "/" + fileName + ".png"));
                    if (!dir.exists()) {
                        dir.mkdir();
                        bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    } else {
                        bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    fos.flush();
                    fos.close();
                }
            } else {
                Toast.makeText(AddPicActivity.this, "未检测到您的SD卡,长时间使用本软件,会造成系统空间减少", Toast.LENGTH_SHORT).show();
            }
        }
    }

       

  • 相关阅读:
    MSSQL错误1069解决方法
    Xamarin的Mono for Android目前可运行于MIPS上
    Oracle大数据机和连接器产品支持与Hadoop和Cloudera Manager集成
    百度1
    恒生电子
    IGT中国
    经典笔试题——a和&a有什么区别
    腾讯HTTP协议1
    腾讯http协议2
    面向对象的三大特征
  • 原文地址:https://www.cnblogs.com/xs104/p/5111111.html
Copyright © 2011-2022 走看看