2012-06-21 13 views
17

万が一、誰かが電話のフォトギャラリーにアクセスする方法を知っていますか? 私は植物の葉を撮るアプリケーションを作成しており、 は画像を分析してそれが決定しているかどうかを判断します。我々は、 が、ユーザーに、葉の写真を撮るか、またはユーザーが既に取っている の葉の画像を使用するという2つの選択肢をユーザに与えることを望んでいた。しかし、私たちは写真を撮っていますが、私たちは 写真ギャラリーへのアクセス方法はわかりません。電話のフォトギャラリーから画像にアクセスする方法は?

答えて

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のコメント: "このアクセス許可は、プラットフォームのドキュメント管理アプリケーションによってのみ要求されます。このアクセス許可は、サードパーティのアプリケーションには付与できません。" –

関連する問題