zoukankan      html  css  js  c++  java
  • Android从相册中获取图片以及路径

    首先是相册图片的获取:

    private final String IMAGE_TYPE = "image/*";

    private final int IMAGE_CODE = 0;   //这里的IMAGE_CODE是自己任意定义的

    //使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片

    Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);

    getAlbum.setType(IMAGE_TYPE);

    startActivityForResult(getAlbum, IMAGE_CODE);

    //重写onActivityResult以获得你需要的信息

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data){

        if (resultCode != RESULT_OK) {        //此处的 RESULT_OK 是系统自定义得一个常量

            Log.e(TAG,"ActivityResult resultCode error");

            return;

        }

        Bitmap bm = null;

        //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口

        ContentResolver resolver = getContentResolver();

        //此处的用于判断接收的Activity是不是你想要的那个

        if (requestCode == IMAGE_CODE) {

            try {

                Uri originalUri = data.getData();        //获得图片的uri 

                bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);        //显得到bitmap图片

    这里开始的第二部分,获取图片的路径:

                String[] proj = {MediaStore.Images.Media.DATA};

                //好像是android多媒体数据库的封装接口,具体的看Android文档

                Cursor cursor = managedQuery(originalUri, proj, null, null, null); 

                //按我个人理解 这个是获得用户选择的图片的索引值

                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                //将光标移至开头 ,这个很重要,不小心很容易引起越界

                cursor.moveToFirst();

                //最后根据索引值获取图片路径

                String path = cursor.getString(column_index);

            }catch (IOException e) {

                Log.e(TAG,e.toString()); 

            }

        }

    }

    点击添加  按钮选择一张图片,显示效果如下:图片下方为图片的绝对路径以及名字

    1.Activity源码:

    package com.jun.activity;

    import java.io.IOException;

    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class PictureActivity extends Activity {
        private final String IMAGE_TYPE = "image/*";

        private final int IMAGE_CODE = 0;   //这里的IMAGE_CODE是自己任意定义的
        
        private Button addPic=null,showPicPath=null;
        
        private ImageView imgShow=null;
        
        private TextView imgPath=null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_picture);
            init();
        }

        private void init() {
            // TODO Auto-generated method stub
            
            addPic=(Button) findViewById(R.id.btnClose);
            showPicPath=(Button) findViewById(R.id.btnSend);
            imgPath=(TextView) findViewById(R.id.img_path);
            imgShow=(ImageView) findViewById(R.id.imgShow);
            
            addPic.setOnClickListener(listener);
            
            showPicPath.setOnClickListener(listener);
            
        }
    private OnClickListener listener=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            
            
            Button btn=(Button) v; 
            
            switch(btn.getId()){
            
            case R.id.btnClose:
                setImage();
                break;
                
        case R.id.btnSend:
            
                break;
            }
            
        }

        

        private void setImage() {
            // TODO Auto-generated method stub
             //使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片

             

            Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);

            getAlbum.setType(IMAGE_TYPE);

            startActivityForResult(getAlbum, IMAGE_CODE);
            
            
        }};
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_picture, menu);
            return true;
        }
        
         protected void onActivityResult(int requestCode, int resultCode, Intent data){

                if (resultCode != RESULT_OK) {        //此处的 RESULT_OK 是系统自定义得一个常量

                    Log.e("TAG->onresult","ActivityResult resultCode error");

                    return;

                }

             

                Bitmap bm = null;

             

                //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口

                ContentResolver resolver = getContentResolver();

             

                //此处的用于判断接收的Activity是不是你想要的那个

                if (requestCode == IMAGE_CODE) {

                    try {

                        Uri originalUri = data.getData();        //获得图片的uri 

             

                        bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);        
                        //显得到bitmap图片
                        imgShow.setImageBitmap(bm);
                        

    //    这里开始的第二部分,获取图片的路径:

             

                        String[] proj = {MediaStore.Images.Media.DATA};

             

                        //好像是android多媒体数据库的封装接口,具体的看Android文档

                        Cursor cursor = managedQuery(originalUri, proj, null, null, null); 

                        //按我个人理解 这个是获得用户选择的图片的索引值

                        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                        //将光标移至开头 ,这个很重要,不小心很容易引起越界

                        cursor.moveToFirst();

                        //最后根据索引值获取图片路径

                        String path = cursor.getString(column_index);
                        imgPath.setText(path);
                    }catch (IOException e) {

                        Log.e("TAG-->Error",e.toString()); 

                    }

                }

            }
    }

    2.xml布局文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

       <RelativeLayout
            android:id="@+id/rlTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/titlebar_bg_nor" >

            <Button
                android:id="@+id/btnClose"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="6dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/bg_btn"
                android:paddingBottom="2dp"
                android:text="@string/Add"
                android:textColor="#fff"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/picture_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
               android:text="@string/picture_title"
                android:textColor="#000"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnSend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="6dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/bg_btn"
                android:paddingBottom="2dp"
                android:text="@string/show"
                android:textColor="#fff"
                android:textSize="12sp" />
        </RelativeLayout>
        
       <ImageView 
           android:id="@+id/imgShow"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_margin="15dip"
           android:background="@drawable/bg_img_coner"
           android:src="@drawable/b"
             android:layout_below="@+id/rlTitle"
           android:scaleType="fitXY"
           />
        
       <TextView 
           android:id="@+id/img_path"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_below="@+id/imgShow"
           android:layout_margin="15dip"
           android:hint="图片路径"
          />

    </RelativeLayout>

    http://blog.csdn.net/qq435757399/article/details/8118528

  • 相关阅读:
    .NET WinForm 状态栏添加分隔符
    使用 MMC / IE 查看证书
    配置IIS在64位Windows上运行 32 位 ASP.NET 应用程序
    WCF部署到IIS:证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权限
    PB6 调用 .net Web Service
    .NET程序加壳 — 之动态加载程序集
    statusStrip 状态条 toolStripStatusLabel 居右显示
    C# 使用 Stopwatch 测量代码运行时间
    Application_Start 事件中使用 Response.Redirect
    解决IIS中部署WCF时,访问.svc文件的404错误问题
  • 原文地址:https://www.cnblogs.com/daishuguang/p/3865704.html
Copyright © 2011-2022 走看看