从相册选取照片时获取路径

我们可以借助一个隐式Intent跳转到相册来选取图片,有时候不单单是为了显示到界面上还需要获取图片文件所在的路径。

首先在AndroidManifest.mxl中声明所需的权限

1
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

通过startActivityForResult()跳转

1
2
3
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, CAMERA_CHOOSE);

从相册选取照片后,在onActivityResult()里对返回的数据进行处理

1
2
3
Uri uri = data.getData();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ivChoose.setImageBitmap(bitmap);

完整代码如下

  • MainActivity中代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.hui.camerapicpath;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_CHOOSE = 0;
private static final String TAG = "MainActivity";
private TextView tvPath;
private ImageView ivChoose;
private Button btnChoose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvPath = (TextView) findViewById(R.id.tv_path);
ivChoose = (ImageView) findViewById(R.id.iv_choose);
btnChoose = (Button) findViewById(R.id.btn_choose);
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, CAMERA_CHOOSE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = null;
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CHOOSE) {
Uri uri = data.getData();
Log.d(TAG, "onActivityResult: uri" + uri);
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ivChoose.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
tvPath.setText(uri.getPath());
}
}
}
}
  • 布局文件activity_main中的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hui.camerapicpath.MainActivity"
android:gravity="center">
<TextView
android:id="@+id/tv_path"
android:textSize="22sp"
android:textColor="#000"
android:text="文件路径:"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv_choose"
android:layout_width="300dp"
android:layout_height="400dp" />
<Button
android:id="@+id/btn_choose"
android:text="打开相册"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
  • 运行程序,成功选取照片后,显示如下

img