首页 > 其他分享 >Android笔记-跳转到相册选择图片

Android笔记-跳转到相册选择图片

时间:2023-02-23 00:33:24浏览次数:31  
标签:相册 photo iv result 跳转 import Android android

跳转到相册选择图片

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

具体的实现步骤:

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

<?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;
        }
    }
}

结果展示

页面呈现:

点击事件的呈现:

标签:相册,photo,iv,result,跳转,import,Android,android
From: https://www.cnblogs.com/liuzijin/p/17144822.html

相关文章

  • Android学习——控件TextView
    1.基本2.带阴影的3.跑马灯效果......
  • 关于首次接触Android Studio
    今天正式接触了AndroidStudio,刚开始学,创建一个安卓工程这个东西  下载太慢。      此时千万不能断网,不然一切都会重新开始。 ......
  • Vue学习:实现用户没有登陆时,访问后自动跳转登录页面
    设计思路定义路由的时候配置属性,这里使用needLogin标记访问页面是否需要登录设置路由守卫,每个页面在跳转之前都要经过验证,校验用户信息是否存在,不存在跳转到登录页用户......
  • Android 启动优化(二) - 有向无环图的原理以及解题思路
    Android启动优化(一)-有向无环图Android启动优化(二)-拓扑排序的原理以及解题思路Android启动优化(三)-AnchorTask使用说明Android启动优化(四)-手把手教你实现An......
  • 【Android】“你好,世界”的地基
    哪一个活动?创建一个普通的BaseActivity类,并将其定为所有活动类的父类。publicclassBaseActivityextendsAppCompatActivity{@OverrideprotectedvoidonC......
  • Android Studio获取MD5、SHA1
    很多的时候都需要获取Android的MD5、SHA1值,像高德、百度地图就是例子需要这个值做密钥。下面Androidstudio工具获取方法1、点击studio面板右上角Gradle,找到app>Tas......
  • Android Studio第一个程序的开发
    1:首先创建一个device,创建成功后运行,就会显示text的内容,为了修饰,打开code,编写代码修改手机app,遇到一个问题就是在xml编写android:background="#f00">无效,在TextView中修改t......
  • Android studio模拟器设置悬浮
    File->Settings->Tools->Emulator,如图这里不要勾选,重启,点击三角形运行项目,弹出来了  ......
  • Android预览超长图原图并且可以滑动查看超长图
    概述:根据需求方的要求,app中预览大图需要显示高清图片。且支持高清预览横向长图(原图)和纵向长图(原图)。且图片可以自由的放大缩小,并且超长图默认需要撑满横屏或者竖屏,可以......
  • Android中drawable和mipmap到底有什么区别
    欢迎通过我的个人博客来查看此文章老项目代码中发现有的图片放到了drawable中,有的图片放到了mipmap中,开发时秉承哪个目录下文件多放哪里的原则,偶尔有疑惑搜一搜文......