GridView android中的Handlle图像方向

时间:2015-04-23 12:45:19

标签: android gridview

我使用下面的代码在GridView中显示设备图像 但是我在GridView中显示它时会出现图像方向问题。

某些图像以水平方向模式显示

like this

public class MultiImagePicActivity extends Activity {

GridView mGrid;

static public String[] arrPath,modifiedArrayPath;
private int ids[];
private int count;
static int selectCount;
Activity act=this;
Context ctx=this;
ArrayList<String> imagepaths;
public static int count1 = 0;
public static String classs;
static int maximageselection;
static int width,height;
//int sel=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState);
    //now get Intent Extras
    Bundle extras;

    if (savedInstanceState == null) 

    {

    //fetching extra data passed with intents in a Bundle type variable

    extras = getIntent().getExtras();

    if(extras == null)

    {        maximageselection= 0;
    classs=null;
    }

    else

    {            /* fetching the string passed with intent using ‘extras’*/

        maximageselection= extras.getInt("maximage");
        classs=extras.getString("From");

    }

    }


    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    width = size.x;
    height = size.y;
    imagepaths=new ArrayList<String>();
    imagepaths.clear();
    loadApps();

    setContentView(R.layout.grid_1);
    mGrid = (GridView) findViewById(R.id.myGrid);

    mGrid.setAdapter(new ImageAdapterxtends());

    mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
    mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener());

}

    private void loadApps() {




    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;
    Cursor imagecursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
    this.count = imagecursor.getCount();
    this.arrPath = new String[this.count];
    ids = new int[count];
    for (int i = 0; i <this.count; i++) {
        imagecursor.moveToPosition(i);
        ids[i] = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        arrPath[i] = imagecursor.getString(dataColumnIndex);
    }

    imagecursor.close();

}


@Override
public void onBackPressed() {
    setResult(Activity.RESULT_CANCELED);
    super.onBackPressed();

}
public class ImageAdapterxtends extends BaseAdapter{
    CheckableLayout l;
    ImageView i;

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arrPath.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arrPath[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            i = new ImageView(MultiImagePicActivity.this);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
            l = new CheckableLayout(MultiImagePicActivity.this);
            l.setLayoutParams(new GridView.LayoutParams(
                    GridView.LayoutParams.WRAP_CONTENT,
                    GridView.LayoutParams.WRAP_CONTENT));
            l.addView(i);
        } else {
            l = (CheckableLayout) convertView;
            i = (ImageView) l.getChildAt(0);
        }
        try {
            setBitmap(i, ids[position]);

        } catch (Throwable e) {
        }
        return l;

    }


    }

    private void setBitmap(final ImageView iv,final int id) {

        new AsyncTask<Void, Void, Bitmap>() {
            Bitmap myBitmap;
            @Override
            protected Bitmap doInBackground(Void... params) {
                return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                super.onPostExecute(result);
                //iv.setImageBitmap(result);
                setMyBitmap(result);
            }
            public final void setMyBitmap(Bitmap bitmap) {

                  if (this.myBitmap != null) {
                    this.myBitmap.recycle();
                  }
                  this.myBitmap = bitmap;
                  iv.getLayoutParams().height = width/3;
                  iv.getLayoutParams().width = width/3;
                  iv.setImageBitmap(myBitmap);
                }
        }.execute();
    }





public class CheckableLayout extends FrameLayout implements Checkable {
    private boolean mChecked;

    public CheckableLayout(Context context) {
        super(context);

    }

    public void setChecked(boolean checked) {

        mChecked = checked;
        setForeground(checked ? getResources().getDrawable(R.drawable.tr) : null);

    }

    public boolean isChecked() {
        return mChecked;
    }

    public void toggle() {
        setChecked(!mChecked);
    }

}

public class MultiChoiceModeListener implements
        GridView.MultiChoiceModeListener {
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        //return true;
        mode.setTitle("Select Items");
        mode.setSubtitle("One item selected");
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
            case R.id.menu_ok:
                if(classs.equals("SliderExistingDetailedActivity")){
            Intent i = new Intent();
                modifiedArrayPath=imagepaths.toArray(new String[imagepaths.size()]);
                i.putExtra("selectedImagepath", modifiedArrayPath);
                //i.putExtra("data", selectedPath);
                setResult(Activity.RESULT_OK, i);
                finish();
                }else{
                    Intent intent=new Intent(MultiImagePicActivity.this, AudioSelectActivity.class);
                     intent.putExtra("IMAGEPATHS", imagepaths);
                    startActivity(intent);
                    finish();
                }
                return true;
            default:
                return false;
        }
    }

    public void onDestroyActionMode(ActionMode mode) {
        Toast.makeText(getApplicationContext(), "text", 1);
    }

    public void onItemCheckedStateChanged(ActionMode mode, int position,
            long id, boolean checked) {
        selectCount = mGrid.getCheckedItemCount();
        if(checked){
            imagepaths.add(arrPath[position]);
            if(selectCount>maximageselection){

                imagepaths.remove(arrPath[position]);
                new AlertDialog.Builder(MultiImagePicActivity.this)
                .setTitle("Restriction")
                .setMessage("You can add at max "+maximageselection+" images !!!")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { 
                        // continue with delete
                        mGrid.refreshDrawableState();
                        dialog.dismiss();
                    }
                 })
                .setIcon(android.R.drawable.ic_dialog_alert)
                 .show();

            }           
        }
        else{
            imagepaths.remove(arrPath[position]);
        }

        switch (selectCount) {
        case 1:
            mode.setSubtitle("One item selected");
            break;
        default:
            mode.setSubtitle("" + selectCount + " items selected");
            break;
        }
    }
    //}
}
}

0 个答案:

没有答案