차근차근/Android

앨범에서 사진 선택, 동영상 선택

예쁜꽃이피었으면 2014. 8. 28. 17:59

http://susemi99.tistory.com/887


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
private final int SELECT_IMAGE = 1;
private final int SELECT_MOVIE = 2;
// 이미지 선택
private void doSelectImage()
{
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("image/*");
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    try
    {
        startActivityForResult(i, SELECT_IMAGE);
    } catch (android.content.ActivityNotFoundException e)
    {
        e.printStackTrace();
    }
}
 
// 동영상선택
private void doSelectMovie()
{
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("video/*");
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    try
    {
        startActivityForResult(i, SELECT_MOVIE);
    } catch (android.content.ActivityNotFoundException e)
    {
        e.printStackTrace();
    }
}
 
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    super.onActivityResult(requestCode, resultCode, intent);
     
    if (resultCode == RESULT_OK)
    {
        if (requestCode == SELECT_IMAGE)
        {
            Uri uri = intent.getData();
            String path = getPath(uri);
            String name = getName(uri);
            String uriId = getUriId(uri);
            Log.e("###", "실제경로 : " + path + "\n파일명 : " + name + "\nuri : " + uri.toString() + "\nuri id : " + uriId);
        }
        else if (requestCode == SELECT_MOVIE)
        {
            Uri uri = intent.getData();
            String path = getPath(uri);
            String name = getName(uri);
            String uriId = getUriId(uri);
            Log.e("###", "실제경로 : " + path + "\n파일명 : " + name + "\nuri : " + uri.toString() + "\nuri id : " + uriId);
        }
    }
}
 
// 실제 경로 찾기
private String getPath(Uri uri)
{
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
 
// 파일명 찾기
private String getName(Uri uri)
{
    String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DISPLAY_NAME);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
 
// uri 아이디 찾기
private String getUriId(Uri uri)
{
    String[] projection = { MediaStore.Images.ImageColumns._ID };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}


반응형