日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android学习笔记进阶十三获得本地全部照片

發布時間:2025/4/9 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习笔记进阶十三获得本地全部照片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是Intent的一個用法。

在ActivityAction里面有一個“ACTION_GET_CONTENT”字符串常量,該常量讓用戶選擇特定類型的數據。

intent.setType("image/*");? 選擇本地所有的圖片。

返回該數據的URI.我們利用該常量生成該圖片的位圖Bitmap,然后為添加到圖片控件(ImageView)上就行了。

選擇你想要的圖片:

?

?

main.xml

[java] view plaincopy
  • <?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>???

  • ?

    [java] view plaincopy
  • 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);????
  • ?????}????
  • }????

  • ?

    ?

    ?

    源代碼:點擊打開鏈接

    轉載于:https://www.cnblogs.com/Free-Thinker/p/6722060.html

    總結

    以上是生活随笔為你收集整理的Android学习笔记进阶十三获得本地全部照片的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。