2012-04-03 20 views
0

私は自分の携帯電話から写真を画面に表示する必要があります。私は現在、電話の写真のすべてのパスを取得しています(下記参照)。今私はそれらを画面に表示する必要があります。何か案は?arraylistの画像を表示<String>(画像のパス名)

ArrayList<String> photoPaths = new ArrayList<String>();   
photoPaths = getAllPhotos(Environment.getExternalStorageDirectory(), photoPaths); 
    Log.e(ACCESSIBILITY_SERVICE, "photo array!"+photoPaths); 
    Log.e(ACCESSIBILITY_SERVICE, "photo path size"+photoPaths.size()); 

    for(int i=0;i<photoPaths.size();i++) 
    { 
     File imgFile = new File(photoPaths.get(i)); 
     if(imgFile.exists()) 
     { 

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

ImageView myImage = (ImageView) findViewById(R.id.picimageView); 
myImage.setImageBitmap(myBitmap);//NEED TO DISPLAY ALL PICTURES 

     } 
+0

ImageViewに複数の画像を表示したいですか? –

+0

私のすべての画像のリストを表示したい – user182192

答えて

0

以下に、SDカードに保存されている画像にアクセスして表示する方法のAndroidの例を示します。

主な考え方は、内部と外部の両方のストレージデバイス(SDカードなど)で使用可能なすべてのメディアのデータを含むメディアプロバイダであるMediaStoreクラスを使用することです。アダプターは、データとビューの間のブリッジとして使用されます。

package blog.android.sdcard; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.AdapterView.OnItemClickListener; 

/** 
* Displays images from an SD card. 
*/ 
public class SDCardImagesActivity extends Activity { 

    /** 
    * Cursor used to access the results from querying for images on the SD card. 
    */ 
    private Cursor cursor; 
    /* 
    * Column index for the Thumbnails Image IDs. 
    */ 
    private int columnIndex; 

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

     // Set up an array of the Thumbnail Image ID column we want 
     String[] projection = {MediaStore.Images.Thumbnails._ID}; 
     // Create the cursor pointing to the SDCard 
     cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
       projection, // Which columns to return 
       null,  // Return all rows 
       null, 
       MediaStore.Images.Thumbnails.IMAGE_ID); 
     // Get the column index of the Thumbnails Image ID 
     columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

     GridView sdcardImages = (GridView) findViewById(R.id.sdcard); 
     sdcardImages.setAdapter(new ImageAdapter(this)); 

     // Set up a click listener 
     sdcardImages.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView parent, View v, int position, long id) { 
       // Get the data location of the image 
       String[] projection = {MediaStore.Images.Media.DATA}; 
       cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
         projection, // Which columns to return 
         null,  // Return all rows 
         null, 
         null); 
       columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToPosition(position); 
       // Get image filename 
       String imagePath = cursor.getString(columnIndex); 
       // Use this path to do further processing, i.e. full screen display 
      } 
     }); 
    } 

    /** 
    * Adapter for our image files. 
    */ 
    private class ImageAdapter extends BaseAdapter { 

     private Context context; 

     public ImageAdapter(Context localContext) { 
      context = localContext; 
     } 

     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); 
       // Move cursor to current position 
       cursor.moveToPosition(position); 
       // Get the current value for the requested column 
       int imageID = cursor.getInt(columnIndex); 
       // Set the content of the image based on the provided URI 
       picturesView.setImageURI(Uri.withAppendedPath(
         MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID)); 
       picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
       picturesView.setPadding(8, 8, 8, 8); 
       picturesView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
      } 
      else { 
       picturesView = (ImageView)convertView; 
      } 
      return picturesView; 
     } 
    } 
} 

主活動のレイアウトを以下に示す:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/sdcard" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:numColumns="auto_fit" 
    android:columnWidth="90dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 

EDIT: をSDカードからサムネイル画像を表示するためのベールであるために、Androidは、最初にそれらを作成する必要がそのため、あらかじめインストールされているGalleryアプリケーションを起動して、sdcardフォルダを開き、sdcardに保存されている画像のサムネイルを自動的に作成する必要があります。これは将来のリリースで修正されるSDKの現在の欠点です(http://groups.google.com/group/android-developers/browse_thread/thread/3f01b284e2537312/fa9487d19db4907e)。あなたは上記のコードの前のバージョンのように

MediaStore.Images.Thumbnails.IMAGE_ID

を使用する場合、いくつかの理由で

は、画像が常に画面上に表示されていません。

に変更MediaStore.Images.Thumbnails._ID

は、問題を解決するようです。私はなぜもっと見て、あなたに戻ってきます。 さらに、一部の画像に間違ったパスが付いています。私は...お楽しみ

cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
projection, // Which columns to return 
null,  // Return all rows 
null, 
MediaStore.Images.Thumbnails.IMAGE_ID); 

cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
projection, // Which columns to return 
null,  // Return all rows 
null, 
null); 

からカーソルオブジェクトの作成を変更しました!

+0

ありがとう!私はこれを試しましたが、私は黒い画面を取得しています....任意のアイデア? – user182192

+0

編集された答えを見つけてください。 – Nimit