2012-06-21 2 views
17

언제든지 전화 사진 갤러리에 액세스하는 방법을 아는 사람이 있습니까? 식물 잎 사진을 찍는 신청을하고 는 심상의 분석 여부를 결정합니다. 우리는 이 잎의 사진을 찍거나 사용자가 이미 가지고있는 잎 잎의 이미지를 사용하는 두 가지 옵션을 사용자에게 제공 할 수 있기를 희망했습니다. 그러나, 우리는 그림을 가져 오게했다. 그러나 우리 do not는 사진 갤러리에 접근하는 방법을 알고있다 .휴대 전화의 사진 갤러리에서 이미지에 액세스하는 방법은 무엇입니까?

답변

32

내장 된 Intents을 사용하여 갤러리 앱을 실행해야합니다. 그 후, 당신의 onActivityResult()에 선택한 이미지의 경로를 얻을하고 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" 
> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
/> 
<Button 
android:id="@+id/loadimage" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Load Image" 
/> 
<TextView 
android:id="@+id/targeturi" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
<ImageView 
android:id="@+id/targetimage" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/> 
</LinearLayout> 

귀하의 활동

package com.exercise.AndroidSelectImage; 

    import java.io.FileNotFoundException; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.ImageView; 
    import android.widget.TextView; 

    public class AndroidSelectImage extends Activity { 

    TextView textTargetUri; 
    ImageView targetImage; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button buttonLoadImage = (Button)findViewById(R.id.loadimage); 
     textTargetUri = (TextView)findViewById(R.id.targeturi); 
     targetImage = (ImageView)findViewById(R.id.targetimage); 

     buttonLoadImage.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     Intent intent = new Intent(Intent.ACTION_PICK, 
     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(intent, 0); 
    }}); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK){ 
    Uri targetUri = data.getData(); 
    textTargetUri.setText(targetUri.toString()); 
    Bitmap bitmap; 
    try { 
     bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri)); 
     targetImage.setImageBitmap(bitmap); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 
    } 
    } 

enter image description here

14

마로 이미지를로드 AndroidManifest.xml에 다음 권한을 추가하는 것을 잊지 마십시오 :

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

임 실은 업로드 된 이미지를 배경으로 넣으 려합니다. 어떻게 알아? – nothingness

+0

READ_EXTERNAL_STORAGE는 WRITE_EXTERNAL_STORAGE가없는 경우에만 필요합니다. MANAGE_DOCUMENTS의 의미가 확실합니까? https://developer.android.com/reference/android/Manifest.permission.html : "이 권한은 플랫폼 문서 관리 앱에서만 요청해야하며 타사 앱에는이 권한을 부여 할 수 없습니다." –