My android imageView is black no matter what - android

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.

Related

Set Button Text On Create not set text as define

I have a code to change text as on create
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button btnbut = FindViewById<Button>(Resource.Id.btnbut);
TextView txtvw = FindViewById<TextView>(Resource.Id.txtView);
txtvw.Text = "Hello World";
btnbut.Text = "Hello WOrld";
}
}
My main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1">
<TextView
android:layout_width="match_parent"
android:layout_height="47.5dp"
android:id="#+id/txtView"
android:layout_marginLeft="1.0dp"
android:layout_marginRight="0.0dp" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnbut" />
</LinearLayout>
When I run and open app with emulator the Text has remain the same as define on main.axml not the text define OnCreate.
Your code is correct and should result in:
Try performing a clean all/build all on your solution and retry your app on the emulator again.
Note: If it is still not updating the app on the emulator correctly:
Within the emulator:
Setting / Apps / Select your app / *Uninstall*
i think i got your i dont use xamrain but i think this is the error try changing
SetContentView(Resource.Layout.Main);
to
SetContentView(Resource.Layout.My main);
coz your layout name is "My main" not "main"

Picasso Main Errored

I am trying to use Picasso Android library but i'm not able to get it working.
For starting, i am trying the simplest of the things:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_view);
mImageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).setIndicatorsEnabled(true);
Picasso.with(this).setLoggingEnabled(true);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(mImageView);
}
the activity layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"/>
</RelativeLayout>
The view remains completely red.
If i set a resource in the view via setImageResource, it correctly displays.
The only relevant clue that something went wrong is a message saying:
Picasso: main errored
but i can't understand why.
I would happily paste more logcat output but it breaks the code block and becomes unreadable.
Here's a pastebin: main errored log
I believe there's something trivial i am doing wrong.
To download images from the internet, you will need the INTERNET permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />

Automatically Generating java code for xml layout in Eclipse

I have got an idea to get rid of some coding when we do initializion of views.
When we create an xml layout in android. At that movement same name class is created with UpperCase Letters with a dialogue with permission. And as i created views with ids. It should create Views and initialize them automatically.
for e.g
close.xml
<?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" >
<Button
android:id="#+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/close_img" />
<TextView
android:id="#+id/closeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing Text" />
</LinearLayout>
And automatically generated java code should be.
public class Close extends Activity
{
TextView CloseText;
Button CloseBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseText = (TextView)findViewById(R.id.closeText);
CloseBtn = (Button)findViewById(R.id.closeBtn);
}
}
I can read xml and do the other stuff as I explained. But how could i add this module to eclipse and How could i create a service which work in Background all the time in eclipse.
Suggest me how to add this module(Plugin) to eclipse and i am going to use JAXB to generate Java Objects from XML document. Is there any better option.
Here in this website i find that just paste the xml and you will get your java code ready for activity class.
i had attached the link

How to reduce the memory of bitmapdrawable in Android 4.0?

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.

Error inflating class TouchImageView

I'm trying to spruce up my image viewer activity by converting from an ImageView to a TouchImageView, per the answer here: How can I get zoom functionality for images?
However, I get an error when my activity is attempting to load:
Binary XML file line #15: Error inflating class TouchImageView
Here's my image_viewer.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
-->
<TouchImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
And my code inside onCreate of ImageViewer.java (though I'm pretty sure that execution never even gets this far):
if(filepath != null && filepath.length() > 0 && new File(filepath).exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(filepath);
TouchImageView imgView = (TouchImageView) findViewById(R.id.imageView1);
imgView.setImageBitmap(myBitmap);
}
Any ideas as to why or what is causing this error? What I saw was the inner most exception/cause. Not sure how I can get more details about the error.
Thanks in advance.
UPDATE:
Exception gets thrown on execution of this line:
setContentView(R.layout.image_viewer);
Further detail on the error:
java.lang.ClassNotFoundException: android.view.TouchImageView in
loader dalvik.system.PathClassLoader#44bfde70
You need to have the fully qualified name to any custom view classes. e.g:
<com.kon.thing.TouchImageView ... />
The JVM can't find your class.
make sure u have entered a correct package name in xml
like..
<com.gallery.TouchImageView
android:id="#+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />

Categories

Resources