java.lang.OutOfMemoryError in android while getting image from gallery in android

后端 未结 4 1112
走了就别回头了
走了就别回头了 2020-12-30 11:32

I am picking a picture from gallery using code

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCont         


        
4条回答
  •  说谎
    说谎 (楼主)
    2020-12-30 12:08

    try this way...

    @Override
     protected void onActivityResult(int requestCode, int resultcode, Intent intent)
     {
         int orientation =0;
         super.onActivityResult(requestCode, resultcode, intent);
    
         if (requestCode == 1)
         {
             if (intent != null && resultcode == RESULT_OK)
             {             
    
                   Uri selectedImage = intent.getData();
    
                   String[] filePathColumn = {MediaStore.Images.Media.DATA};
                   Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                   cursor.moveToFirst();
                   int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                   String filePath = cursor.getString(columnIndex);
                   Log.v("log","filePath is : "+filePath);
    
                   cursor.close();
                   try 
                   {
                        ExifInterface exif = new ExifInterface(filePath);
                         orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                        //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
                        Log.v("log", "ort is "+orientation);
    
                   } 
                   catch (IOException e)
                   {
                       e.printStackTrace();
                   }
    
                   if(bmp != null && !bmp.isRecycled())
                   {
                       bmp = null;               
                   }
    
                   File f = new File(filePath);
    
                   bmp = decodeFile(f,300,420);
           }
         }
       }
    

    and this is the decode function...

    public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
         try {
             //Decode image size
             BitmapFactory.Options o = new BitmapFactory.Options();
             o.inJustDecodeBounds = true;
             BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    
             //The new size we want to scale to
             final int REQUIRED_WIDTH=WIDTH;
             final int REQUIRED_HIGHT=HIGHT;
             //Find the correct scale value. It should be the power of 2.
             int scale=1;
             while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                 scale*=2;
    
             //Decode with inSampleSize
             BitmapFactory.Options o2 = new BitmapFactory.Options();
             o2.inSampleSize=scale;
             return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
         } catch (FileNotFoundException e) {}
         return null;
     }
    

提交回复
热议问题