Android显示目录中的图像

时间:2015-11-06 13:51:00

标签: java android android-layout dynamic android-imageview

我正在尝试在图库类型活动中显示Android设备上指定目录中的图像。我正在关注(至少尝试)关于在an​​droid开发者网站上显示图像的指南,但一直迷路http://developer.android.com/training/displaying-bitmaps/load-bitmap.html。我目前有一个缩减采样版本的图像,并将其加载到layout.xml文件中的图像视图中。但这需要我知道将出现的图像数量,我不会,我担心这是浪费。

我需要知道如何根据目录中的图像数量来创建图像视图。并在创建的imageViews中显示缩略图或下采样图像。

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" 
       tools:context=".MainActivity">
    <TextView android:text="Hello World!" android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/textView" />

    <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/mImageView0"
       android:layout_centerHorizontal="true"
       android:layout_alignParentEnd="true" />
    ............
    <ImageView
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mImageView5"
        android:layout_below="@id/mImageView4"
        android:layout_centerHorizontal="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    private final String TAG = "MainActivity Class";

    public ImageView mImageView;
    public ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String fileDir = Environment.getExternalStorageDirectory().toString()+ File.separator +"Camera-App-Images"+ File.separator;
        File f = new File(fileDir);
        Log.d(TAG, fileDir);
        File file[] = f.listFiles();

        ImageView[] imgViews = new ImageView[file.length];

        for (int i=0; i < file.length; i++) {
            mImageView =  (ImageView)findViewById(R.id.mImageView0 + i);
            listView = (ListView)findViewById(R.id.listView);
            loadBitmap(fileDir + file[i].getName(), mImageView);
        }
    }

    public void loadBitmap(String fileLoc, ImageView imageView) {
        BitmapWorkerTask task = new BitmapWorkerTask(fileLoc ,imageView);
        task.execute();
    }
}

BitmapWorkerTask

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;
    private int data = 0;

    private String imagePath = "";

    private final String TAG = "BitmapWorkerTask";

    public BitmapWorkerTask(String imgPath, ImageView imageView) {
        imagePath = imgPath;
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);

        Log.d(TAG,"Constructor");
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Integer... params) {
         //data = params[0];
         return decodeSampledBitmapFromFile(imagePath, 100, 100);
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
             final ImageView imageView = imageViewReference.get();
             if (imageView != null) {
                imageView.setImageBitmap(bitmap);

            }
        }
    }

    public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

         if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }
}

0 个答案:

没有答案
相关问题