从SD卡Android读取图像时自动旋转

时间:2013-04-02 10:32:47

标签: android

我要溢出,这是我的第一个问题:)

我有一个图库,图像切换器和用于从相机捕获图像的按钮。 将图像捕获到SD卡后,我想将图像加载到图库,并将其显示在图像切换对象中。现在它可以解决一个大问题。

为什么所有图像都在图库和图像切换器中旋转?

这是我的java代码:

protected void onCreate(Bundle savedInstanceState) {
   iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
        iSwitcher.setFactory(this);
        iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        setAllPics();
        gallery = (Gallery) findViewById(R.id.Gallery01);
        gallery.setAdapter(new ImageAdapter(this)); 
        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                //iSwitcher.setImageResource(pics[arg2]);
                //iSwitcher.setImageURI(mUrls[arg2]);
                iSwitcher.setImageDrawable(new BitmapDrawable(getResources(),(bitmap[arg2])));
            }
        });
        Button BtnGetPhoto = (Button) this.findViewById(R.id.BtnGetPicture);
}
public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int mGalleryItemBackground;
        public ImageAdapter(Context c) {
            ctx = c;
        }

        @Override
        public int getCount() {
            return mUrls.length;
            //return pics.length;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            ImageView iView = new ImageView(ctx);
            //iView.setImageURI(mUrls[arg0]);
            iView.setImageDrawable(new BitmapDrawable(getResources(),(bitmap[arg0])));
            //iView.setImageResource(pics[arg0]);
            iView.setScaleType(ImageView.ScaleType.FIT_XY);
            iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
            return iView;
        }
    }
private void setAllPics(){
        File images  = new File(PATH);
        File[] imagelist = images.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                 return ((filename.endsWith(".jpg"))||(filename.endsWith(".png")));  
            }
        });
        /* There isn't enough memory to open up more than a couple camera photos */
        /* So pre-scale the target bitmap into which the file is decoded */

        /* Get the size of the ImageView */
        int targetW = 350;//iSwitcher.getWidth();
        int targetH = 220;//iSwitcher.getHeight();


        BitmapFactory.Options bmOptions = new BitmapFactory.Options();



        mFiles = new String[imagelist.length];
        int photoW[] = null;
        int photoH[] = null; 
        int scaleFactor[] = null ;

        photoW = new int[imagelist.length];
        photoH = new int[imagelist.length];
        scaleFactor = new int[imagelist.length];
        bitmap = new Bitmap[imagelist.length];
        for(int i= 0 ; i< imagelist.length; i++)  
            {               
                bmOptions.inJustDecodeBounds = true;
                mFiles[i] = imagelist[i].getAbsolutePath();  
                bitmap[i] = BitmapFactory.decodeFile(mFiles[i], bmOptions);
                /* Get the size of the image */
                photoW[i] = bmOptions.outWidth;
                photoH[i] = bmOptions.outHeight;
                /* Figure out which way needs to be reduced less */

                if((targetW > 0)  || (targetH > 0 ))
                    scaleFactor[i]  = Math.min(photoW[i]/targetW, photoH[i]/targetH);
                /* Set bitmap options to scale the image decode target */
                bmOptions.inJustDecodeBounds = false;
                bmOptions.inSampleSize = scaleFactor[i];
                bmOptions.inPurgeable = true;
                try{
                /* Decode the JPEG file into a Bitmap */
                bitmap[i] = BitmapFactory.decodeFile(mFiles[i], bmOptions);
                //iSwitcher.setImageDrawable(new BitmapDrawable(getResources(),(bitmap[i])));
                } catch (OutOfMemoryError error) {
                    error.printStackTrace();
                }

            }  


        mUrls = new Uri[mFiles.length];  

        for(int i=0; i < mFiles.length; i++)  
                {  
                    mUrls[i] = Uri.parse(mFiles[i]);     
                }


    }
private OnClickListener onClickListener =new OnClickListener(){ 

    // @Override
     public void onClick(View v) {

         switch (v.getId()){
             case R.id.BtnGetPicture:

                 Intent cameraIntent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                 String imageFileName = "";
                 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());


                 SetPathIfNotExist(PATH);
                 imageFileName = timeStamp + ".jpg";

                 File image = new File(PATH,imageFileName);
                 Uri uriSavedImage = Uri.fromFile(image);
                 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

                 startActivityForResult(cameraIntent,CAMERA_REQUEST);
                     break;
                        default:

                break;
         }          

     }    
     };
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
        setAllPics();
        gallery.setAdapter(new ImageAdapter(this)); 

        //Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        //imageView.setImageBitmap(photo);
    }  
} 

1 个答案:

答案 0 :(得分:1)

Hi have a look at this below code. before saving your captured image do the following process. it will save the images in portrait mode. hope this will help you.

 int rotation = -1;
 rotation = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getOrientation();



    Matrix rotator = new Matrix();
    switch (rotation) {
    case (Surface.ROTATION_0):
        break;
    case (Surface.ROTATION_90):
        rotator.postRotate(270);
        break;
    case (Surface.ROTATION_180):
        rotator.postRotate(180);
        break;
    case (Surface.ROTATION_270):
        rotator.postRotate(90);
        break;


    // screen_{width,height} are applied before the rotate, so we don't
    // need to change them based on rotation.
    bmp_ss = Bitmap.createBitmap(bmp_ss, 0, 0, screen_width, screen_height, rotator, false);