2

In my app I'm displaying images from galley and on selection of image i want to upload that image to web server.For uploading image to server I'm using following code but I'm getting error at bitmapImage = BitmapFactory.decodeFile(path,opt);

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();
        }
android
  • 396
  • 1
  • 8
  • 20

2 Answers2

15

Error is due to the size of the image, i used this code to decrease the size of image when select from gallery.

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;

    }

i hope this may helps you.

Abhi
  • 8,593
  • 7
  • 34
  • 60
  • @abhi...thanks for your reply..but I'm still getting error..1 small question...after getting bitmap i.e. **Bitmap bitmap = Bitmap Factory.decode File(file Path, o2); ** after this how do u convert this into base 64 string...I'm using following code for this bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); data = baos.toByteArray(); str=Base64.encodeBytes(data); – android Sep 13 '11 at 11:31
  • @abhi...thanks i have sorted out that problem...now its working fine...thank u so much... – android Sep 13 '11 at 12:00
  • @Abhi I used your code and it solved almost my error thanks for that but I facing one problem because of this method. This method is rotating my original image. And that I dont want. I want to display my image like showing in Gallery. Is there any way to do this? – anddev Jun 10 '13 at 05:21
  • Hi @anddev There is no code to rotare image in above function, It will just scale the image. check at your end – Abhi Jun 10 '13 at 05:30
  • @Abhi thanks for quick reply but I checked from my end using different method and still some of the image will rotate and display into imageview. So the problem is not regarding the above code that my mistake. I am sorry for that. But now can you plz suggest me the right way for regarding my problem? Thanks – anddev Jun 10 '13 at 05:42
  • Use ExifInterface See [this SO asnwer](http://stackoverflow.com/a/13340090/792232) – Abhi Jun 10 '13 at 06:26
0

You have to break in to samples while loading the image, there is already a question and a good answer for it, have a look at this page , this might help you.

Strange out of memory issue while loading an image to a Bitmap object

Community
  • 1
  • 1
Yashwanth Kumar
  • 27,303
  • 13
  • 62
  • 68