开发者

Null-pointer issue displaying an image from assets folder Android 2.2 SDK

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);

        returnedIm开发者_StackOverflow社区age = 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;

    }


Where is "file:///android_asset/Samples/" + imageName coming from? If your hierarchy looks like assets/file_name.jpg, you would just call open(file_name.jpg). In other words, try replacing your file:///android_asset/Samples/" + imageName with just imageName.

Check out the API Demos, specifically the ReadAsset.java class:

try {
        InputStream is = getAssets().open("read_asset.txt");

...

where the assets folder looks like

Null-pointer issue displaying an image from assets folder Android 2.2 SDK


Frank I had the same problem !

I had my PNG images within "assets" of my project and AssetManager.open() kept on giving me an exception because it couldn't find the file !

I investigated by using assetManager.list("") to list what's in "assets". I subsequently discovered that my images were actually NOT added to the "assets" !

As you can imagine I was getting pretty pissed off at this point because obviously my images should have been within assets because I could see them play as day in Eclipse within the damn "assets" folder of my project.

Solution

  1. back-up the files that are in assets folder of your project. I used Windows Explorer for this drag-drop operation.
  2. go back to eclipse and delete your files within "assets". Use Eclipse for this so that you don't need to refresh your project.
  3. get your Windows Explorer window back and drag your backed-up files into Eclipse and onto "assets". Your cursor changes to a "+". When you let go of your mouse button Eclipse will prompt you if you want to link or copy. Select copy.
  4. rebuild your project and the images are now truly in assets.

Bonus - I updated your getBitmapFromAsset() method:

private Bitmap getBitmapFromAsset(String strName) throws IOException
{
    AssetManager assetManager = getAssets();

    InputStream istr = assetManager.open(strName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    return bitmap;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜