How to reduce the memory of bitmapdrawable in Android 4.0? - android

I have an image existed in device sdcard.
The path is /mnt/sdcard/001.jpg
Here is my activity
public class BitmapMemoryTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imv = (ImageView)findViewById(R.id.imv);
imv.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/001.jpg"));
}
}
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imv" />
</LinearLayout>
I test this project on Android 3.0 and Android 4.0 and dump the hprof.
The bitmapdrawable memory in the ImageView on Android 4.0 is bigger than 3.0
Android 3.0 retains heap is 3,692,864 byte
Android 4.0 retains heap is 7,385,320 byte
How to solve this problem?
P.S I don't want to down sample the image.

Related

Can Android layout be loaded in the C language?

This link says that to make the app display a layout, you create a main_layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
and then load it in onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// main_layout.xml is compiled into the R.layout.main_layout object
setContentView(R.layout.main_layout);
}
My problem is that I am coding in C, and have the C version of "onCreate" method:
JNIEXPORT
void ANativeActivity_onCreate(ANativeActivity *activity, void *savedState,
size_t savedStateSize) {
....
}
is it possible to load the layout and make the app display it in the C language?
C uses "ANativeActivity" that it doesn't has any of Java methods and resources/objects. Even if you manually parse that XML you should MANUALLY implement all Widgets/Components in C.....so it's near impossible due to high amount of work involved in it.
Usually someone chooses C on Android to do "some special work" that is not available on normal Java or due to performance issues on it.

How to display two videos simultaneously with surface view

I am a beginner in android development.
I was trying to display a video with surface view using media codec for which i am successful.
Now I want to add one more video at run time which has to be displayed or hidden as per the wish of the user or to switch between the two.
Can I have some suggestions for the same...
Thanks......
Try this
public class CustomPictureActivity extends Activity {
/** Called when the activity is first created. */
VideoView vd1,vd2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vd1=(VideoView) findViewById(R.id.v1);
vd2=(VideoView) findViewById(R.id.v2);
vd1.setVideoURI(Uri.parse("/mnt/sdcard/file.mp4"));
vd1.setMediaController(new MediaController(this));
vd1.requestFocus();
vd1.start();
vd2.setVideoURI(Uri.parse("/mnt/sdcard/android.mp4"));
vd2.setMediaController(new MediaController(this));
vd2.requestFocus();
vd2.start();
}
}
Your xml code should be like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v1"/>
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v2"/>
</LinearLayout>
May this will help you.

My android imageView is black no matter what

I try to display an ImageView into my app on a well (hopefully) defined view (XML)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activityShowLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#73C310"
android:orientation="vertical"
tools:context=".ShowActivity" >
<ImageView
android:id="#+id/mainImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/moodButton"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="12dp"
android:background="#000"
android:src="#drawable/gs_04_pic" />
<!-- ... -->
and after that in my onCreate method on the activity
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
/* ... blabla bla ... */
ImageView imageView = (ImageView) findViewById(R.id.mainImage);
Bitmap bitmap = BitmapFactory.decodeByteArray(cream.getMedia(), 0, cream.getMedia().length);
Drawable draw = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(draw);
imageView.invalidate();
}
with cream.getMedia() returning a valid byte[] (from database)
which I can log giving me:
07-18 17:41:16.812: I/app(9883): byte[] is: [B#414af0b0
But at the end the imageView is black (no image on it)
If I don't run the onCreate code it displays the resource set in the XML (on black background as set in XML too)
What is wrong?
I run into this problem, in my case the problem came from the config in:
bluid.gradle
shrinkResources true
It deleted my files because I didn't have a static reference to my resources. (images in my case)
so I found this article "Keeping Resources" in this link Resource Shrinking
In resumen it says: you should make a file in this path "/res/raw/keep.xml"
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="#layout/l_used*_c,#layout/l_used_a,#layout/l_used_b*"/>
It may be because of you are trying to load a larger bitmap into the imageview, please try to scale it down before setting.

Samsung GS3 Specific Error s3dReadConfigFile. Works on any other Android device.

So I have a MainActivity that goes like this.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.splash_layout);
ImageView imageView = (ImageView) findViewById(R.id.logo);
imageView.setImageResource(R.drawable.logo_p);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run() {
jump();
}
}, 3000);
}
With a Layout like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingTop="5sp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="5sp"
/>
When I debug it on a Samsung Galaxy S2 (Android 4.0.3), the image appears and the jump function is successfully called. When I debug it on a Samsung Galaxy S3 (Android 4.1.2), LogCat throws this error.
05-18 19:51:05.832: E/(20438): <s3dReadConfigFile:75>: Can't open file for reading
The jump function is never called and the app crashes completely. I do not have any idea what this error is and how to fix it. This question has also been asked here but has no answers as of this writing.
The application works as long as the imageview is not set a source. If I comment out this line I still get the error but the jump function is called. If I don't call setContentView() at all, the error never appears.

Unable to use example class posted here in my project

I have been trying everything to get this to work, but to no avail. I tried to use the (AutoResizeTextView) posted here: Auto Scale TextView Text to Fit within Bounds
I created a new class file in my project with the name "AutoResizeTextView" and pasted the above code.
Then i opened the main.xml file and put n the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.mn.rl.AutoResizeTextView android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
in the mainActivity.java file, i have the following code:
package com.mn.rl;
import android.app.Activity;
import android.os.Bundle;
public class rlActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
com.mn.rl.AutoResizeTextView txt = (com.mn.rl.AutoResizeTextView) findViewById(R.id.tv);
txt.setText("Hello");
}
}
I have no errors in the code, and it runs but there is no autoresizing. the text remains the same size. in the xml i have the autoresize textview layout_width and layout_height set to fill parent, yet the font remains small. also tried txt.resize().
What am i doing wrong? Please Help.
there is a slight possibility that that class is only meant for resizing down. not up.

Categories

Resources