2011-06-23 18 views
0

SDカードの画像をギャラリーに表示しようとしていて、ギャラリー内の画像を選択すると、それをより大きなImageViewに表示しようとしています。私はギャラリーを正常に実装しましたが(下記のコードを参照してください)、カーソルとString []で作業した後、OnItemClickListenerでImageViewで選択したイメージを表示する方法を理解できませんでした。 :ImageViewでカーソルから画像を表示する方法は? - Android

import java.io.File; 
import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.res.TypedArray; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Base64; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Toast; 


public class Send extends Activity { 


    private Cursor cursor; 
    private int columnIndex; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sendlayout); 


     final String[] projection = {MediaStore.Images.Thumbnails._ID}; 

     cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
       projection, null, null, MediaStore.Images.Thumbnails.IMAGE_ID); 

     columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

     final ImageView imageView = (ImageView) findViewById(R.id.imageView10); 
     imageView.setImageResource(R.raw.blackinscreen); 


     final Gallery sdcardImages = (Gallery) findViewById(R.id.gallery10); 
     sdcardImages.setAdapter(new ImageAdapter(this)); 

     sdcardImages.setOnItemClickListener(new OnItemClickListener() 
     { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) { 


      } 

     }); 

    } 


    private class ImageAdapter extends BaseAdapter { 

     private Context context; 
     private int itemBackground; 

     public ImageAdapter(Context localContext) { 
      context = localContext; 

      TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
      itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 
      a.recycle(); 
     } 

     public int getCount() { 
      return cursor.getCount(); 
     } 
     public Object getItem(int position) { 
      return position; 
     } 
     public long getItemId(int position) { 
      return position; 
     } 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView picturesView; 
      if (convertView == null) { 
       picturesView = new ImageView(context); 
       cursor.moveToPosition(position); 
       int imageID = cursor.getInt(columnIndex); 
       picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID)); 
       picturesView.setScaleType(ImageView.ScaleType.FIT_XY); 
       picturesView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
       picturesView.setBackgroundResource(itemBackground); 
      } 
      else { 
       picturesView = (ImageView)convertView; 
      } 
      return picturesView; 
     } 
    } 
} 

xmlの上部にギャラリーがあり、その下に画像が表示されます。このコードでは、ギャラリーから選択したイメージをImageViewにどのように表示するのですか?あなたの時間をありがとう。

答えて

0

私はその後、その後、対応する項目

ImageAdapter imageAdapter = (ImageAdapter) arg0.getAdapter(); 

または

ImageAdapter imageAdapter = sdcardImages.getAdapter(); 

を取得後、あなたのOnItemClickListenerであなたのギャラリーのアダプタを取得することができ、あなたのアダプタ
上のどこかにあなたのIMAGEIDを保存することができrecommand以前に保存したimageIDを取得できます(getItemメソッドを適用する必要があります)。

int imageID = imageAdapter.getItem(position); 

あなたはImageViewソースを変更することをお勧めします:)

関連する問題