thiet-ke-web-gia-re

Ứng dụng hiển thị ảnh trên Smartphone

Đầu tiên chúng ta tiến hành tạo một project mới Slider01 sau đó mở activity_main.xml và code như sau :Screen Shot 2015-06-22 at 1.02.02 PM

 Tiến hành tạo mới layout row.xml với nội dung :

Screen Shot 2015-06-22 at 1.08.05 PM

Mở MainActivity.java và code như sau :

package com.example.mac.slider01;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.CursorLoader;
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.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import java.io.LineNumberReader;


public class MainActivity extends Activity {
    final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
    final String thumb_IMAGE_DATA = MediaStore.Images.Thumbnails.DATA;
    final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;
    private GridView myGridView;
    private MyAdapter mySimpleCursorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myGridView = (GridView) findViewById(R.id.gridView);
        String[] from = {MediaStore.MediaColumns.TITLE};
        int[] to = {android.R.id.text1};
        CursorLoader cursorLoader = new CursorLoader(this,sourceUri,null,null,null, MediaStore.Images.Media.TITLE);
        Cursor cursor = cursorLoader.loadInBackground();
        mySimpleCursorAdapter= new MyAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        myGridView.setAdapter(mySimpleCursorAdapter);

        AdapterView.OnItemClickListener myOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0,View view,int position,long arg3) {
                Cursor cursor = mySimpleCursorAdapter.getCursor();
                cursor.moveToPosition(position);
                int int_ID = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
                getThumbnail(int_ID);
            }
        };

        myGridView.setOnItemClickListener(myOnItemClickListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public Bitmap getThumbnail(int int_ID)
    {
        String[] thumbColumn = {thumb_IMAGE_DATA,thumb_IMAGE_ID};
        CursorLoader thumbCursorLoader = new CursorLoader(this,thumbUri,thumbColumn,thumb_IMAGE_ID+"="+int_ID,null,null);
        Cursor thumbCursor = thumbCursorLoader.loadInBackground();
        Bitmap thumbBitmap = null;
        if(thumbCursor.moveToFirst())
        {
            int thColumnIndex = thumbCursor.getColumnIndex(thumb_IMAGE_DATA);
            String thumbPath = thumbCursor.getString(thColumnIndex);
            Toast.makeText(getApplicationContext(),thumbPath,Toast.LENGTH_LONG).show();
            thumbBitmap = BitmapFactory.decodeFile(thumbPath);

            AlertDialog.Builder thumbDialog = new AlertDialog.Builder(MainActivity.this);
            ImageView thumbView = new ImageView(MainActivity.this);
            thumbView.setImageBitmap(thumbBitmap);
            LinearLayout layout = new LinearLayout(MainActivity.this);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.addView(thumbView);
            thumbDialog.setView(layout);
            thumbDialog.show();
        }
        else
        {
            Toast.makeText(getApplicationContext(),"No thumbnail",Toast.LENGTH_LONG).show();
        }
        return thumbBitmap;
    }

    public class MyAdapter extends SimpleCursorAdapter
    {
        private Cursor myCursor;
        private Context myContext;

        MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flag) {
            super(context, layout, c, from, to, flag);
            myCursor = c;
            myContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.row, parent, false);
            }
            ImageView thumbv = (ImageView) row.findViewById(R.id.thumbnail);
            //move to next image
            myCursor.moveToPosition(position);
            int myID = myCursor.getInt(myCursor.getColumnIndex(MediaStore.Images.Media._ID));
            String[] thumbColumns = {thumb_IMAGE_DATA, thumb_IMAGE_ID};
            CursorLoader thumbCursorLoader = new CursorLoader(myContext, thumbUri, thumbColumns, thumb_IMAGE_ID + "=" + myID, null, null);
            Cursor thumbCursor = thumbCursorLoader.loadInBackground();
            Bitmap myBitmap = null;
            if (thumbCursor.moveToFirst()) {
                int thColumnIndex = thumbCursor.getColumnIndex(thumb_IMAGE_DATA);
                String thumbPath = thumbCursor.getString(thColumnIndex);
                myBitmap = BitmapFactory.decodeFile(thumbPath);
                thumbv.setImageBitmap(myBitmap);
            }
            return row;
        }
    }
}

 


Chèn thêm code permission vào AndroidManifest.xml như sau :

Screen_Shot_2015-06-22_at_1_14_09_PM

 

Vậy là ok bây giờ các bạn có thể chạy thử app rồi đó

Một vài hình ảnh sau khi chạy thành công :

 

Add Comment