如何在Android上以原始质量在ImageView上显示大图像文件?

时间:2015-03-21 14:42:43

标签: java android socket.io base64

我正在尝试发送和显示图像文件。我可以显示和编码/解码大约50kb(低于50kb)的图像。但是当我尝试发送/编码大于100kb的图像时,我收到一个错误:"java.lang.OutOfMemoryError"

            Uri selectedImage = intent.getData();

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

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

            bmp = BitmapFactory.decodeFile(filePath);

我使用this codes获取文件路径。 我将位图转换为字节数组,将字节数组转换为base64string。我正在使用socketio将basese64string发送到服务器。我在ImageView中显示位图。但是当文件大于50kb时,base64编码和imageview不起作用。我怎么解决这个问题?
我试过android:largeHeap="true"但没有解决我的问题

提前谢谢你

UDATE
位图到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;
}    }

字节数组到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;
}    }

更新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)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题://但是我有图像问题的质量。因此当使用blurred显示图像时质量下降。图像为original size 我希望有人在这方面可以提供帮助..

      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<-