Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3305870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:13:14+00:00 2026-05-17T21:13:14+00:00

I looked at the 2 examples on Stack, but can’t get them to work.

  • 0

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;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:13:15+00:00Added an answer on May 17, 2026 at 9:13 pm

    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

    alt text

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.