2012-06-21 11 views
17

Czy ktoś wie, jak uzyskać dostęp do galerii zdjęć telefonu? Tworzę aplikację, która wykonuje zdjęcie liścia rośliny, a analizuje obraz w celu ustalenia, czy jest on określony. Mieliśmy nadzieję, że my, , możemy dać użytkownikowi dwie możliwości wykonania zdjęcia liścia lub skorzystania z obrazu z liścia, który użytkownik już zrobił. Jednak mamy zdjęcie biorące udział, ale nie mamy wiedzieć, jak uzyskać dostęp do galerii zdjęć.Jak uzyskać dostęp do obrazu z galerii zdjęć telefonu?

Odpowiedz

32

Musisz uruchomić aplikację Galeria za pomocą wbudowanego Intents. Po tym, na twojej onActivityResult(), uzyskać ścieżkę wybranego obrazu i załadować obraz do swojego 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> 

swoją aktywność

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

Do nie zapomnij dodać następujących uprawnień do AndroidManifest.xml:

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

Próbuję umieścić przesłany obraz jako tło, wiesz jak? – nothingness

+0

READ_EXTERNAL_STORAGE jest potrzebny tylko, jeśli nie masz jeszcze WRITE_EXTERNAL_STORAGE. Czy jesteś pewien, że MANAGE_DOCUMENTS ma sens? https://developer.android.com/reference/android/Manifest.permission.html mówi: "Zezwolenie to powinno być wymagane tylko przez aplikację do zarządzania dokumentacją platformy. To uprawnienie nie może zostać przyznane aplikacjom innych firm." –

Powiązane problemy