登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 880|回复: 0

【Android教程】获取本地图片并显示

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-6-20 20:22:38 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. android获取本地图片并显示图片
    6 ]$ Z7 z+ c( ~. t' L! t; v& }
  2. 8 ^+ @3 d! w  u7 w4 U$ U
  3. import java.io.FileNotFoundException;7 {" T2 ^5 D6 ^! ]8 [' i: P

  4.   k* {" x7 x6 ]% \5 n1 R  y
  5. import android.content.ContentResolver;
    / p4 M. G  v# P0 C2 J2 j. ]
  6. import android.content.Intent;
    # a& q! ^, Z' k% N2 D. X+ }( j6 ^
  7. import android.graphics.Bitmap;3 P; K' z7 I: Q4 c( ~3 P" M6 m/ O
  8. import android.graphics.BitmapFactory;# e9 \& J- s1 {) x# u
  9. import android.net.Uri;" K4 M5 S0 H3 R
  10. import android.os.Bundle;. j3 V8 s' _4 n4 z
  11. import android.util.Log;
    9 S' U# e8 m1 `8 k$ k
  12. import android.view.View;+ N+ ?- ?6 D, a4 f; C% \# f
  13. import android.view.View.OnClickListener;* {% i. J* e  H
  14. import android.widget.ImageView;1 [- X6 }$ Q# _, u# _
  15. " ~# ?$ o, m- B3 _! J
  16. import com.maikefengchao.daixu.R;
    : X. v. s; Q3 m8 Z: v
  17. 1 D7 f7 s$ D) M/ q: v" ^) a
  18. public class WriteArticle_CompeterelayActivity extends BaseActivity {
    ! R, Z3 V$ T0 I, R% h8 n
  19.     private ImageView im_upload_img;
    8 P$ e- p) A5 X- S. r
  20. / L# ?+ M5 d% ^2 B# A5 c
  21.     @Override
    1 g3 A# e; H" O+ t
  22.     public void initView(Bundle savedInstanceState){7 D1 k$ d9 K* I5 ~
  23.         setContentView(R.layout.view_write_competerelay);
    + b7 l1 e6 L; n" a
  24. 4 f4 U$ o! T% v" T$ K
  25.         im_upload_img = (ImageView)findViewById(R.id.write_competerelay_cover_iv);' g5 J6 ?8 G6 f# n2 N8 x- [6 ^
  26.     }: {1 G, }9 P2 @' T( m, T
  27. ( e& U: h6 W9 `# B+ X
  28.     @Override
    " _$ I3 b1 c5 f  j+ Q4 d3 `1 Z
  29.     protected void setListener() {; j( @2 I( r4 u- Y; |$ h/ ?$ k
  30.         im_upload_img.setOnClickListener(new OnClickListener() {* ~- `* M& {& g/ [- r
  31.             @Override5 _% L+ L8 u$ P3 y1 P% l
  32.             public void onClick(View v) {9 ?5 T# q% J- j" L+ u
  33.                 Intent intent = new Intent();
    ' Z$ G' t# m0 ?* p# ?& u
  34.                 /* 开启Pictures画面Type设定为image */
    + _- D4 G( I  _, M! y3 O
  35.                 intent.setType("image/*");
    9 F/ }6 M5 N. q1 G/ \
  36.                 /* 使用Intent.ACTION_GET_CONTENT这个Action */
    * A8 T6 R# E: W6 F5 `
  37.                 intent.setAction(Intent.ACTION_GET_CONTENT);
    " k$ g' @7 B/ C, _0 k4 W+ O
  38.                 /* 取得相片后返回本画面 */* U+ j8 f3 O7 |* O; i& K
  39.                 startActivityForResult(intent, 1);: j7 s& p' a# l2 ?6 ]
  40.             }7 a$ S1 n$ \4 [7 H8 T7 n
  41.         });0 u0 t! B3 W& y; ]3 ]9 k
  42.     }' C  w, ]# g  A$ @

  43. 4 `0 C, x5 w, \
  44.     @Override
    ( c- w: i- ]8 |. ~- f# B) B- ?
  45.     protected void processLogic(Bundle saveInstanceState) {# i' K2 t& S% n! r: H
  46. 0 D& g# x+ r( z1 h6 v, g: D
  47.     }6 `& \4 U+ X1 n

  48. ! z' d& ^1 a9 |

  49. ( o8 I8 v1 s7 J
  50.     //获取本地图片
    ) `7 ~: x. q! P
  51.     @Override
    ' }! C1 o7 M) s6 Y- T! T& o
  52.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {, i- h5 y$ Q' O
  53.         if (resultCode == RESULT_OK) {% m9 N  ?! g% p$ E4 Y0 U
  54.             Uri uri = data.getData();
    , V# O7 m' ~1 l/ d
  55.             String img_url = uri.getPath();//这是本机的图片路径
    1 i+ M( ~8 i0 f$ o( H
  56.             ContentResolver cr = this.getContentResolver();; w9 V. b( B2 B0 N$ x1 E
  57.             try {% U9 t4 Y. }, y6 h4 ]8 K2 i# w
  58.                 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
    : r4 u* R4 Z+ k8 N" e7 u# C
  59.                 ImageView imageView = (ImageView) findViewById(R.id.write_competerelay_cover_iv);
    " N% E. t* j: s# v- P
  60.                 /* 将Bitmap设定到ImageView */# g% a$ Z9 r( o
  61.                 imageView.setImageBitmap(bitmap);* U% F7 U& o! Q3 `" T+ m
  62.             } catch (FileNotFoundException e) {
    # c: w2 O! A* U8 E, E, c
  63.                 Log.e("Exception", e.getMessage(),e);2 b3 f+ P5 M6 m4 @
  64.             }
    # T4 N! ~* O% P5 U+ z
  65.         }9 {8 ]# k+ z& ~
  66.         super.onActivityResult(requestCode, resultCode, data);
    / ^* W* v9 n5 e+ v: s
  67.     }
    , }4 j# P3 y- f6 E# A) J# ^6 f. b
  68. }; H/ x% `. G& W# A6 a- ~1 N
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|星空社区 |网站地图

GMT+8, 2024-5-6 11:55 , Processed in 0.073032 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表