2011-06-23 76 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

我建議您到地方保存你的圖像標識上的適配器
然後你就可以得到您的畫廊的適配器上的OnItemClickListener然後得到相應的項目

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

ImageAdapter imageAdapter = sdcardImages.getAdapter(); 

然後你可以得到你以前存儲的imageID(你必須調整你的getItem方法):

int imageID = imageAdapter.getItem(position); 

那麼你很好改變你的ImageView源:)

相關問題