Get Bitmap Size Get Free Memory Exception Android
Bitmap size calculation:
bmpHeight * bmpWidth
For example:
Resolution of image 1024x860 = 880 640 pixels
If every pixel get 4 byte of memory:
880 640x4= 3 522 560 (3.5MB)
Get bitmap size without allocation of memory:
Get Memory size:
Make your bitmap not bigger as maxMemory size
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
bmpHeight * bmpWidth
For example:
Resolution of image 1024x860 = 880 640 pixels
If every pixel get 4 byte of memory:
880 640x4= 3 522 560 (3.5MB)
Get bitmap size without allocation of memory:
BitmapFactory.Options options = new BitmapFactory.Options();
// If set to true, the decoder will return null (no bitmap), but the out... fields will still
// be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.my_image, options);
int imageHeight = options.outHeight; // 1024
int imageWidth = options.outWidth; // 860
String imageType = options.outMimeType; // .jpg .png .gif
Get Memory size:
Make your bitmap not bigger as maxMemory size
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
float density = getResources().getDisplayMetrics().density;
Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int freeMemory = (int) (Runtime.getRuntime().freeMemory() / 1024);
String memMessage = String.format(
"Free=%d kB,
MaxMem=%d kB,
Memory: Pss=%.2f MB, Private=%.2f MB, Shared=%.2f MB",
freeMemory,
maxMemory,
memoryInfo.getTotalPss() / 1024.0,
memoryInfo.getTotalPrivateDirty() / 1024.0,
memoryInfo.getTotalSharedDirty() / 1024.0);
((TextView)findViewById(R.id.textViewInfo)).setText(memMessage );
396LW NO topic_id
AD
Další témata ....(Topics)
How change background color of View Android sample.
MainActivity.java
main.xml
MainActivity.java
public class MainActivity extends Activity {
TextView hTextView;
TableRow hTableRow;
Button hButton, hButtonStop;
private Handler mHandler = new Handler();
private int nCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hButton = (Button)findViewById(R.id.idButton);
hButton.setOnClickListener(mButtonStartListener);
hTableRow = (TableRow)findViewById(R.id.idTableRow1);
} // end onCreate
View.OnClickListener mButtonStartListener = new OnClickListener() {
public void onClick(View v) {
hTableRow.setBackgroundColor(Color.YELLOW);
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tableLayout1">
<TableRow android:id="@+id/idTableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#5655AA">
<TextView
android:id="@+id/idTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</TableRow>
</TableLayout>
</LinearLayout>
1.) Open DDMS via Menu Tools > Android > Android Device Monitor
2.) Select Device
3.) In DDMS click Menu Window > Show View > File Exlporer
4.) Device > Storage folder
5.) Emulator > data > data folder
2.) Select Device
3.) In DDMS click Menu Window > Show View > File Exlporer
4.) Device > Storage folder
5.) Emulator > data > data folder
Full Screen without titlebar and statusbar for Android Activity.
In Activity.java
or in AndroidManifest.xml
In Activity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
or in AndroidManifest.xml
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>
String sFileContent = readFile("myfile.txt",StandardCharsets.UTF_8);
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}
// write file
String sOut = "text blah hello world etc.";
writeToFile(sOut"someName.txt");
static void writeToFile(String sB,String name) {
String folder = ("c:\\folder\");
File f = new File(folder+ name);
BufferedWriter writer = null;
writer = new BufferedWriter( new OutputStreamWriter(
new FileOutputStream( folder+name),"UTF-8"));
writer.write( sB);
if ( writer != null)
writer.close( );
}
If you want save battery, select "Satelite map" from map menu and reduce the brightness of the display.
Editace: 2013-12-09 12:58:29
Počet článků v kategorii: 396
Url:get-bitmap-size-get-free-memory-exception-android