Android学习笔记进阶十三获得本地全部照片
这是Intent的一个用法。
在ActivityAction里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据。
intent.setType("image/*"); 选择本地所有的图片。
返回该数据的URI.我们利用该常量生成该图片的位图Bitmap,然后为添加到图片控件(ImageView)上就行了。
选择你想要的图片:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <ImageView
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:scaleType="fitXY"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- package xiaosi.image;
- import java.io.FileNotFoundException;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class ImageActivity extends Activity {
- /** Called when the activity is first created. */
- private Button button = null;
- private ImageView imageView = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.button);
- button.setText("选择图片");
- button.setOnClickListener(new ButtonListener());
- }
- private class ButtonListener implements OnClickListener{
- public void onClick(View v)
- {
- Intent intent = new Intent();
- /* 开启Pictures画面Type设定为image */
- intent.setType("image/*");
- /* 使用Intent.ACTION_GET_CONTENT这个Action */
- intent.setAction(Intent.ACTION_GET_CONTENT);
- /* 取得相片后返回本画面 */
- startActivityForResult(intent, 1);
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode == RESULT_OK) {
- Uri uri = data.getData();
- Log.e("uri", uri.toString());
- ContentResolver contentResolver = this.getContentResolver();
- try {
- Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
- imageView = (ImageView) findViewById(R.id.image);
- /* 将Bitmap设定到ImageView */
- imageView.setImageBitmap(bitmap);
- }
- catch (FileNotFoundException e){
- Log.e("Exception", e.getMessage(),e);
- }
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
源代码:点击打开链接
分类:
android提高篇
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2016-04-17 窥探 Swift 之 函数与闭包的应用实例
2016-04-17 swift 深入理解Swift的闭包
2016-04-17 Swift开发语法
2016-04-17 Swift学习笔记 - 函数与闭包
2016-04-17 那些年,学swift踩过的坑
2014-04-17 android之TabWidget选项卡