[英]How to show large image file on ImageView with original quality in android?
我正在嘗試發送和顯示圖像文件。 我可以顯示和編碼/解碼大約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);
我使用此代碼獲取文件路徑。
我將位圖轉換為字節數組,並將字節數組轉換為base64string。 我正在將basese64string發送到具有socketio的服務器。 我在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)
我用以下方法解決了://但是我有圖像質量問題。因此,當顯示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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.