【问题标题】:How to show large image file on ImageView with original quality in android?如何在 Android 中以原始质量在 ImageView 上显示大图像文件?
【发布时间】:2015-09-26 13:12:42
【问题描述】:

我正在尝试发送和显示图像文件。我可以显示和编码/解码大约 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
位图到字节数组:

 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)

【问题讨论】:

  • 你忘了告诉你什么时候得到 OutOfMemoryError 。哪种说法?
  • 您应该在加载之前调整图像大小。图像消耗的内存基于图像的分辨率
  • 我添加了一些来自 logcat 的错误

标签: java android socket.io base64


【解决方案1】:

我用这个解决了://但是我有图像质量问题。所以当显示带有original size的图像时,质量下降。图像是blurred我希望有人能帮助解决这个问题尊重..

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

【讨论】:

    猜你喜欢
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2013-01-16
    • 2014-09-23
    • 2017-06-06
    相关资源
    最近更新 更多