I looked at the 2 examples on Stack, but can't get them to work. I'm simply trying to grab an image from a folder in assets and set it as in ImageView, but get a null pointer returned. What am I doing wrong?
Main Activity: package com.xxx.xxx;
import java.io.InputStream;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class SamplesViewFlipper extends SamplesViewCreator {
private Bitmap returnedImage;
ImageView imgView;
ViewFlipper vf;
private String imageName = "testImage.png";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
returnedImage = getImageFromAsset(imageName);
imgView = (ImageView) findViewById(R.id.dynamicImageView);
imgView.setImageBitmap(returnedImage); //<-null pointer happens here
vf = (ViewFlipper) findViewById(R.id.SamplesViewFlipper);
setContentView(R.layout.view_flipper_samples);
}
public void buttonClickHandler(View view) {
switch (view.getId()) {
case R.id.nextSampleButton:
vf.showNext();
break;
case R.id.backSampleButton:
vf.showPrevious();
break;
}
}
}
Extender Class:
package com.xxx.xxx;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class SamplesViewCreator extends Activity {
private InputStream is;
private Bitmap bitmap;
public Bitmap getImageFromAsset(String imageName) {
AssetManager mngr = getAssets();
try {
is = mngr.open("file:///android_asset/Samples/" + imageName);
bitmap = BitmapFactory.decodeStream(is);
//also tried "Files/" + imageName per example on Stack
} catch (final IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
And my two xml files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SamplesLayout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/samples_menu"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:background="#0061F9">
<Button android:id="@+id/nextSampleButton"
android:layout_marginRight="10dp"
android:gravity="center"
android:layout_width="50dp"
android:layout_height="30dp"
android:text="Next"
android:textColor="#FFFFFF"
android:textSize="13dp"
android:layout_weight="1"
android:background="@drawable/button"
android:layout_alignParentRight="true"
android:onClick="buttonClickHandlerSamples"/>
<Button android:id="@+id/backSampleButton"
android:layout_marginLeft="10dp"
android:gravity="center"
android:layout_width="50dp"
android:layout_height="30dp"
android:text="Back"
android:textColor="#FFFFFF"
android:textSize="13dp"
android:layout_weight="1"
android:background="@drawable/button"
android:onClick="buttonClickHandler"/>
</RelativeLayout>
<LinearLayout android:id="@+id/SamplesViewFlipperLayout"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ViewFlipper android:id="@+id/SamplesViewFlipper"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<!--adding views to ViewFlipper-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/header_pad_left"
android:paddingRight="@dimen/header_pad_right"
android:paddingBottom="@dimen/header_pad_bot"
android:paddingTop="@dimen/header_pad_top"
android:orientation="vertical"
android:background="@color/background" >
</LinearLayout>
</ViewFlipper>
</Linear
Layout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dynamicImageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/default_samples_image"/>
EDIT:
Also tried this, but still get a file not found exception.
Public Bitmap getImageFromAsset() throws IOException {
try {
is = getAssets().open("test3.png");
bitmap = BitmapFactory.decodeStream(is);
} catch (final IOException e) {
e.printStackTrace();
}
System.out.println("bitmap is " + bitmap);
return bitmap;
}