跳转到相册选择图片

即设置一个点击事件,点击之后即可跳转到相册进行图片的选择

具体的实现步骤:

界面很简单的啦,这里就直接将源代码放出来啦:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tiaoPhoto"
android:orientation="vertical"> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="对方号码:"> </TextView>
<EditText
android:id="@+id/et_hao"
android:layout_width="780px"
android:layout_height="wrap_content"
android:text="10086"> </EditText>
</LinearLayout> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="彩信标题:"> </TextView>
<EditText
android:id="@+id/et_top"
android:layout_width="780px"
android:layout_height="wrap_content"
android:text="下面是打招呼内容"> </EditText>
</LinearLayout> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="彩信内容:"> </TextView>
<EditText
android:id="@+id/et_body"
android:layout_width="780px"
android:layout_height="wrap_content"
android:text="hello"> </EditText>
</LinearLayout> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="图片附件:"> </TextView> <ImageView
android:id="@+id/iv_photo"
android:layout_width="780px"
android:layout_height="400px"> </ImageView> </LinearLayout> <Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送彩信"> </Button> </LinearLayout>

然后就是后台代码了,如下所示:

package com.example.myapplication;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView; public class tiaoPhoto extends AppCompatActivity implements View.OnClickListener {
private ImageView iv_photo;
private ActivityResultLauncher<Intent> resultLauncher; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiao_photo); iv_photo=findViewById(R.id.iv_photo); iv_photo.setOnClickListener(this);//设置点击事件 //跳转到系统相册,选择图片,并返回
resultLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode()==RESULT_OK){
Intent intent=result.getData(); //获得选中文件的路径对象
Uri pUri=intent.getData();
if(pUri!=null){
iv_photo.setImageURI(pUri);
Log.d("ning","pUri"+pUri.toString());
} }
}
}); } @Override
public void onClick(View view) {
switch(view.getId()){
case R.id.iv_photo:
//跳转到系统相册,选择图片,并返回
Intent intent=new Intent(Intent.ACTION_GET_CONTENT); //确定需要的图片类型
intent.setType("image/*"); resultLauncher.launch(intent);//返回之后,跳转到上面的那个方法里面 break;
}
}
}

结果展示

页面呈现:

点击事件的呈现:

Android笔记-跳转到相册选择图片的更多相关文章

  1. 浅谈Android中拍照、从相册选择图片并截图相关知识点

    前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...

  2. [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现

    [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现 今天做Android项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之 ...

  3. HTML5 Plus 拍照或者相册选择图片上传

    HBuilder+HTML5 Plus+MUI实现拍照或者相册选择图片上传,利用HTML5 Plus的Camera.Gallery.IO.Storage和Uploader来实现手机APP拍照或者从相册 ...

  4. IOS研究院之打开照相机与本地相册选择图片(六)

    原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:IOS研究院之打开照相机与本地相册选择图片(六) Hello 大家好 IOS的文章好久都木有更新了,今天更新一篇哈. 这篇文章主要学习 ...

  5. 微信小程序:从本地相册选择图片或使用相机拍照。

    wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 示例代码: wx.chooseImage({ count: 1, // 默认9 sizeTyp ...

  6. Ionic3学习笔记(十二)拍照上传图片以及从相册选择图片上传

    本文为原创文章,转载请标明出处 目录 安装插件 导入 app.module.ts 创建 provider 更多 效果图 1. 安装插件 终端运行: ionic cordova plugin add c ...

  7. 【ARK UI】HarmonyOS 从相册选择图片并显示到Image组件上

    ​ 参考资料 [Harmony OS][ARK UI]ETS 上下文基本操作 [Harmony OS][ARK UI]ets使用startAbility或startAbilityForResult方式 ...

  8. android开发——从相冊中选择图片不裁剪

    转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46864777 问题: 在郭神的第一行代码中,第8章的从相冊中选择图片这块,从相冊选 ...

  9. ng-cordova 手机拍照或从相册选择图片

    1.需求描述 实现一个调用摄像头拍照,或者直接打开本地图库选择照片,然后替换App中图片的功能 2.准备 1) 安装ng-cordova 进入到ionic工程目录,使用bower工具安装, bower ...

  10. IOS研究院之打开照相机与本地相册选择图片

    如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样.最终点击发送将按钮将图片2进制图片上传服务器. 下面我 ...

随机推荐

  1. C#定时任务(Timer)

    新建Timer类 using BaseAsset.Data.Infrastructure; using BaseAsset.Data.Repositories; using BaseAsset.Ent ...

  2. ubuntu64运行32位程序安装过程

    Ubuntu运行32位程序可以使用如下方法: 第一步: 确认你有一个64位架构的内核 你可以打开终端然后输入: dpkg --print-architecture 你将会看到像下面这样的内容: amd ...

  3. 端口被占用 for mac

    启用项目提示端口被占用 解决方案: lsof -i : 3010 杀死占用的进程 kill -9 4804 重新启动

  4. 开发Unity3D移动端输入插件 UGUI Touch Input Component

    UGUI Touch Input Component 为了在移动设备上操控角色,本人便开发了UGUI Touch Input Component输入类插件. 特点 本插件中总共包含三种组件:the v ...

  5. 25 bootstrap--v3--datetimepicker时间选择器--应用

    在模板中引用响应的文件 比如: layout.html <link rel="stylesheet" href="{% static 'stark/plugins/ ...

  6. Java 获取【.jar】文件里的资源文件

    获取jar文件里的图片等文件时,会发现使用相对路径不行了. 因为打包后的jar文件,在获取路径时稍有不同. 下面是获取jar文件中图片的例子: 1 Resource[] resources = new ...

  7. AX2012 data() 和 buf2buf()的区别

    data() 和 buf2buf()都是AX2012 里面可以选择使用的数据拷贝函数.不同的是data会拷贝原始记录里面的所有字段,包括系统字段(公司,创建人等).而buf2buf在拷贝数据时则不会拷 ...

  8. 【APT】Hades APT组织针对乌克兰发起网络攻击事件分析

    背景 Hades一个充满神秘色彩的APT组织,该组织因为2017年12月22日针对韩国平昌冬奥会的攻击活动被首次发现,后来卡巴斯基将该次事件的攻击组织命名为Hades.但是该攻击组织的归属问题却一直未 ...

  9. (论文笔记)Deep Neural Network for YouTube Recommendation

    YouTube推荐系统上的深度神经网络 [总结] 在召回模型中,用到的特征比较粗,在训练过程中,目的是训练出一个用户向量u(通过用户本身的浏览和观看信息和统计学信息,假设是N维的),用户向量的用途分两 ...

  10. 重写mybatis-plus的saveUpdate方法

    重写mybatis-plus的saveUpdate方法 1.问题出现 同步外部数据的时候,如果需要同步逻辑删除的数据,mybatis-plus的saveOrUpdate||saveOrUpdateBa ...