Android -- 打开本地图片且显示路径

简介:

背景                                                                                         

imageimage

image

代码                                                                                          

先上布局文件:

复制代码
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/hello_world"/>
    <ImageView
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name"/>
</LinearLayout>
复制代码

这里没有TextView,我最后是将路径以System.out.println方式输出的。

初始化:

复制代码
button = (Button)findViewById(R.id.button);
        pic = (ImageView) findViewById(R.id.pic);
        button.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                System.out.println("onClick");
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);
            }
        });
复制代码

对于startActivityForResult的回调函数进行覆写:

复制代码
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO 自动生成的方法存根
        System.out.println(requestCode+"");
        if(requestCode==1)
        {
            //获得图片的uri 
            Uri uri = data.getData();
            //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
            ContentResolver cr = this.getContentResolver();
            Bitmap bitmap;
            //Bitmap bm; //这是一种方式去读取图片
            try 
            {
                //bm = MediaStore.Images.Media.getBitmap(cr, uri); 
                //pic.setImageBitmap(bm);
                bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                pic.setImageBitmap(bitmap);
                System.out.println("GOOD");
                //第一种方式去读取路径
                //String[] proj = {MediaStore.Images.Media.DATA};
                /*
                 //好像是android多媒体数据库的封装接口,具体的看Android文档
                Cursor cursor = managedQuery(uri, proj, null, null, null); 
                //按我个人理解 这个是获得用户选择的图片的索引值
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
              //将光标移至开头 ,这个很重要,不小心很容易引起越界
                cursor.moveToFirst();
                //最后根据索引值获取图片路径
                String path = cursor.getString(column_index);
                System.out.println(path);
                   */
                //第二种方式去读取路径
                Cursor cursor =this.getContentResolver().query(uri, null, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String path = cursor.getString(column_index);
                System.out.println(path);                
            } 
            catch (Exception e)
            {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
                System.out.println("BAD");
            }
            
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
复制代码

最后的path就是路径。

我是天王盖地虎的分割线                                 




本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/3720594.html,如需转载请自行联系原作者

相关文章
Android通过手势(多点)缩放和拖拽图片
Android通过手势(多点)缩放和拖拽图片
99 4
android 下载图片的问题
android 下载图片的问题
78 3
Android经典面试题之图片Bitmap怎么做优化
本文介绍了图片相关的内存优化方法,包括分辨率适配、图片压缩与缓存。文中详细讲解了如何根据不同分辨率放置图片资源,避免图片拉伸变形;并通过示例代码展示了使用`BitmapFactory.Options`进行图片压缩的具体步骤。此外,还介绍了Glide等第三方库如何利用LRU算法实现高效图片缓存。
107 20
Android经典面试题之图片Bitmap怎么做优化
Android平台RTMP推送|轻量级RTSP服务|GB28181接入之文字、png图片水印的精进之路
本文探讨了Android平台上推流模块中添加文字与PNG水印的技术演进。自2015年起,为了满足应急指挥及安防领域的需求,逐步发展出三代水印技术:第一代为静态文字与图像水印;第二代实现了动态更新水印内容的能力,例如实时位置与时间信息;至第三代,则优化了数据传输效率,直接使用Bitmap对象传递水印数据至JNI层,减少了内存拷贝次数。这些迭代不仅提升了用户体验和技术效率,也体现了开发者追求极致与不断创新的精神。
Android经典实战之如何获取图片的经纬度以及如何根据经纬度获取对应的地点名称
本文介绍如何在Android中从图片提取地理位置信息并转换为地址。首先利用`ExifInterface`获取图片内的经纬度,然后通过`Geocoder`将经纬度转为地址。注意操作需在子线程进行且考虑多语言支持。
421 4
Android经典实战之Kotlin中实现圆角图片和圆形图片
本文介绍两种实现圆角图像视图的方法。第一种是通过自定义Kotlin `AppCompatImageView`,重写`onDraw`方法使用`Canvas`和`Path`进行圆角剪裁。第二种利用Android Material库中的`ShapeableImageView`,简单配置即可实现圆角效果。两种方法均易于实现且提供动态调整圆角半径的功能。
146 0
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
150 4
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
108 1
Android系统中读写和显示图片
Android系统中读写和显示图片
67 0
|
10月前
|
54. 【Android教程】图片资源:Drawable
54. 【Android教程】图片资源:Drawable
142 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等