2011-09-13 12 views
2

내 앱에서 갤리에서 이미지를 표시하고 이미지를 선택하면 이미지를 웹 서버에 업로드하고 싶습니다. 서버에 이미지를 업로드하려면 다음을 사용하고 있습니다. 코드에서 오류가 발생했습니다. bitmapImage = BitmapFactory.decodeFile (path, opt);java.lang.outofmemoryerror 비트 맵 크기가 비트 맵에서 비트 맵 크기를 초과했습니다

private void uploadImage(String selectedImagePath) { 
     String str = null; 
     byte[] data = null; 
     String Responce= null; 
     Bitmap bitmap2 = null; 
     try { 
      File file=new File(selectedImagePath); 
      //FileInputStream fileInputStream = new FileInputStream(new File(imagePath2)); 
      FileInputStream fileInputStream=new FileInputStream(selectedImagePath); 
      Log.i("Image path 2",""+selectedImagePath+"\n"+fileInputStream); 
      name=file.getName(); 
      name=name.replace(".jpg",""); 
      name=name.concat(sDate).concat(".jpg"); 
      Log.e("debug",""+name); 

      //else 
      //{ 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inTempStorage = new byte[16*1024]; 

      //bitmapImage = BitmapFactory.decodeFile(path,opt); 

      bitmap2=BitmapFactory.decodeFileDescriptor(fd, outPadding, opts) 
      Log.i("Bitmap",""+bitmap.toString()); 
      BitmapFactory.decodeStream(fileInputStream); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bitmap2.compress(Bitmap.CompressFormat.PNG, 100, baos); 
      data = baos.toByteArray(); 
      str=Base64.encodeBytes(data); 
      //} 

      //String image=str.concat(sDate); 
      ArrayList<NameValuePair> nameValuePairs = new 
      ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("image",str)); 
      nameValuePairs.add(new BasicNameValuePair("imagename", name)); 
      Log.e("debug",""+nameValuePairs.toString()); 

      HttpClient client=new DefaultHttpClient(); 
      HttpPost post=new HttpPost("http://ufindfish.b4live.com/uploadTipImage.php"); 

      post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      HttpResponse httpResponse=client.execute(post); 
      HttpEntity entity=httpResponse.getEntity(); 
      InputStream inputStream=entity.getContent(); 

      StringBuffer builder=new StringBuffer(); 
      int ch; 
      while((ch = inputStream.read()) != -1) 
      { 
       builder.append((char)ch); 
      } 
      String s=builder.toString(); 
      Log.i("Response",""+s); 



     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     if(bitmap2!=null) 
     { 
      bitmap2.recycle(); 
     } 

답변

15

에러 이미지의 크기, i가 갤러리에서 선택 이미지의 크기를 감소시키는 코드를 사용할 예정이다.

public Bitmap setImageToImageView(String filePath) 
    { 
    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) 
    { 
    if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
    break; 
    width_tmp /= 2; 
    height_tmp /= 2; 
    scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2); 
    return bitmap; 

    } 

이 정보가 도움이되기를 바랍니다.

+0

@abhi ... 답장을 보내 주셔서 감사합니다.하지만 여전히 오류가 발생합니다 .1 작은 질문 ... 비트 맵 가져 오기 후 ** 비트 맵 비트 맵 = 비트 맵 Factory.decode 파일 (파일 경로, o2); **이 후 어떻게베이스 64 문자열로 변환합니까 ...이 bitmap.compress (Bitmap.CompressFormat.PNG, 100, baos); 다음 코드를 사용하고 있습니다; \t \t \t \t data = baos.toByteArray(); \t \t \t \t str = Base64.encodeBytes (data); – android

+0

@abhi ... 감사합니다. 나는 그 문제를 해결했습니다 ... 지금은 잘 작동합니다 ... 아주 많이 감사드립니다 ... – android

+0

@Abhi 나는 당신의 코드를 사용했고, 그것에 대한 나의 오류를 거의 해결해주었습니다. 이 방법으로 인해 문제가 발생했습니다. 이 방법은 원본 이미지를 회전시킵니다. 그리고 나는 그다지 원하지 않는다. 갤러리에 표시되는 것처럼 내 이미지를 표시하고 싶습니다. 이 일을 할 수있는 방법이 있습니까? – anddev