android.support.v4.app.GalleryFragment.java Source code

Java tutorial

Introduction

Here is the source code for android.support.v4.app.GalleryFragment.java

Source

package android.support.v4.app;

//import android.app.Fragment;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

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

import diep.esc.demo.mycustomgallery.GridViewAdapter;
import diep.esc.demo.mycustomgallery.R;

/**
 * Created by Diep on 20/03/2016.
 */
public class GalleryFragment extends Fragment {
    private static final String TAG = "check";
    private long[] mListOfImageID = null;
    private Bitmap[] mListOfBitmap = null;
    private boolean[] mErrorMask;
    private Bitmap failedIcon = null;
    private ArrayList<String> mFileNames;
    private GridView mGridView;

    public GalleryFragment() {
        mFileNames = new ArrayList<>();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mListOfImageID = savedInstanceState.getLongArray("photoID");
            mFileNames = savedInstanceState.getStringArrayList("fileName");
            mErrorMask = savedInstanceState.getBooleanArray("errMask");
        }
        if (mListOfImageID == null) {
            mListOfImageID = lisAllImageId();
            mErrorMask = new boolean[mListOfImageID.length];
        }
        mListOfBitmap = getThumbnails();
    }

    public void setTag(String tag) {
        mTag = tag;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLongArray("photoID", mListOfImageID);
        outState.putStringArrayList("fileName", mFileNames);
        outState.putBooleanArray("errMask", mErrorMask);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View res = inflater.inflate(R.layout.fragment_gallery, null);
        Log.d("check", "onCreateView ");
        mGridView = (GridView) res.findViewById(R.id.gridView);
        mGridView.setAdapter(new GridViewAdapter(getContext(), R.layout.grid_item, mListOfBitmap, mFileNames));
        mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showImg(position);
                return true;
            }
        });

        return res;
    }

    private void showImg(int index) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(mFileNames.get(index))), "image/*");
        getActivity().startActivity(intent);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        recycleBitmaps();
        super.onDestroy();
    }

    private Bitmap[] getThumbnails() {
        failedIcon = BitmapFactory.decodeResource(getResources(), R.drawable.faild_icon);
        Bitmap[] res = new Bitmap[mListOfImageID.length];
        for (int i = 0; i < res.length; i++) {
            if (!mErrorMask[i]) {
                Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
                        getActivity().getApplicationContext().getContentResolver(), mListOfImageID[i],
                        MediaStore.Images.Thumbnails.MICRO_KIND, null);
                res[i] = bm;
                if (bm == null) {
                    mErrorMask[i] = true;
                    res[i] = failedIcon;
                }
            } else {
                res[i] = failedIcon;
            }
        }
        return res;
    }

    private long[] lisAllImageId() {
        ArrayList<Long> res = new ArrayList<>();
        mFileNames.clear();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String projection[] = new String[] { MediaStore.Images.ImageColumns._ID,
                MediaStore.Images.ImageColumns.DATA };
        Cursor cur = getActivity().getContentResolver().query(uri, projection, null, null, null);
        while (cur.moveToNext()) {
            long val = cur.getLong(0);
            String fName = cur.getString(1);
            if (val >= 0 && new File(fName).exists()) {
                res.add(val);
                mFileNames.add(fName);
            }
        }
        cur.close();
        long[] res_final = new long[res.size()];
        for (int i = 0; i < res_final.length; i++) {
            res_final[i] = (Long) res.get(i);
        }
        return res_final;
    }

    private void recycleBitmaps() {
        if (mListOfBitmap != null) {
            for (int i = 0; i < mListOfBitmap.length; i++) {
                if (mListOfBitmap[i] != null) {
                    mListOfBitmap[i].recycle();
                    mListOfBitmap[i] = null;
                }
            }
        }
        //mListOfBitmap=null;
        if (failedIcon != null && !failedIcon.isRecycled()) {
            failedIcon.recycle();
            //failedIcon=null;
        }
    }
}