0

I'm trying to send and show image file. I can show and encode/decode image about 50kb (lower than 50kb). But when I try to send/encode image bigger than 100kb I get an error: "java.lang.OutOfMemoryError".

            Uri selectedImage = intent.getData();

            String filePath=MediaOperations.getPath(getApplicationContext(), selectedImage);

            if (bmp != null && !bmp.isRecycled()) {
                bmp = null;
            }

            bmp = BitmapFactory.decodeFile(filePath);

I use this codes for getting file path.
I convert bitmap to byte array and byte array to base64string. And I'm sending basese64string to server with socketio. And I am showing bitmap in ImageView. But base64 encoding and imageview doesn't work when file is larger than 50kb. How can I solve this problem?
I tried android:largeHeap="true" but did not solve my problem

Thanks in advance

UDATE
bitmap to byteArray:

 public static byte[] getBytes(Bitmap bitmap) {
   try {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
}    }

byte array to base64String:

   public static String getBase64(byte[] array){
try {
    return Base64.encodeToString(array, Base64.DEFAULT);
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
}    }

UPDATE 2:

  Window is full: requested allocation 15120804 bytes, free space 2096642 bytes, window size 2097152 bytes <br></br>
  java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

ERROR_LOG:

03-21 11:47:24.116: E/dalvikvm-heap(2084): Out of memory on a 30241618-byte allocation.
03-21 11:47:24.120: I/dalvikvm(2084): "main" prio=5 tid=1 RUNNABLE
03-21 11:47:24.124: I/dalvikvm(2084):   | group="main" sCount=0 dsCount=0 obj=0xa4bca480 self=0xb8a84bd0
03-21 11:47:24.128: I/dalvikvm(2084):   | sysTid=2084 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1216913376
03-21 11:47:24.132: I/dalvikvm(2084):   | state=R schedstat=( 9832259071 3939135544 5574 ) utm=726 stm=256 core=0
03-21 11:47:24.136: I/dalvikvm(2084):   at java.lang.String.<init>(String.java:~365)
03-21 11:47:24.140: I/dalvikvm(2084):   at java.lang.String.<init>(String.java:228)
03-21 11:47:24.144: I/dalvikvm(2084):   at android.util.Base64.encodeToString(Base64.java:456)
Community
  • 1
  • 1
erginduran
  • 1,678
  • 5
  • 28
  • 51

1 Answers1

0

I solved with this: // But I have quality of image problems.so that the quality declining.image is blurred when shows image with original size.I hope someone can help in this regard..

      public static Bitmap lessResolution(String filePath) {
    int reqHeight = 120;
    int reqWidth = 120;
    BitmapFactory.Options options = new BitmapFactory.Options();

    // First decode with inJustDecodeBounds=true to check dimensions
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(filePath, options);
}

private static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
}

->for original post<-

Community
  • 1
  • 1
erginduran
  • 1,678
  • 5
  • 28
  • 51