207_intent启动相册

本文介绍如何通过Intent启动Android设备上的相册,并允许用户从中选择一张图片。具体步骤包括创建Intent、设置ACTION_PICK动作及指定类型为'image/*'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


207_intent启动相册



//先创建一个intent
//给一个action,再给一个uri
//actionIntent.ACTION_PICK
//uriandroid.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URIIntent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//然后设置一个type"image/*"
intent.setType("image/*");

//启动activity
startActivityForResult(intent, 0);




这里我们只是启动了相册,还没有什么实际的操作
以后我们再说选择图片的事情



这里我们看一下涉及到的几个源码

首先是 Intent.ACTION_PICK

来看看源码

/**
 * Activity Action: Pick an item from the data, returning what was selected.
 * <p>Input: {@link #getData} is URI containing a directory of data
 * (vnd.android.cursor.dir/*) from which to pick an item.
 * <p>Output: The URI of the item that was picked.
 */
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
public static final String ACTION_PICK = "android.intent.action.PICK";


来翻译一下:
activity action: 从data里面挑选一个项目,返回被选择的那个
Input输入:包含目录的uri

output输出:被选择的项目的uri





再看看
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI

/**
 * The content:// style URI for the "primary" external storage
 * volume.
 */
public static final Uri EXTERNAL_CONTENT_URI =
        getContentUri("external");

/**
 * Get the content:// style URI for the image media table on the
 * given volume.
 *
 * @param volumeName the name of the volume to get the URI for
 * @return the URI to the image media table on the given volume
 */
public static Uri getContentUri(String volumeName) {
    return Uri.parse(CONTENT_AUTHORITY_SLASH + volumeName +
            "/images/media");
}


其实就是指向相册的uri







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值