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

  • Home
  • SEARCH
  • 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 6803735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:22:50+00:00 2026-05-26T19:22:50+00:00

I am able to open my image from SD card in gridview and also

  • 0

I am able to open my image from SD card in gridview and also able to pass image path to another activity by using following codes:

package com.example.sdcardimage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class Imageshow extends Activity {


    private Cursor cursor;
    private int columnIndex;
     TextView text;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);  
       text = (TextView)findViewById(R.id.text);
        // Set up an array of the Thumbnail Image ID column we want
        String[] projection = {MediaStore.Images.Thumbnails._ID};
        // Create the cursor pointing to the SDCard
        cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,
                MediaStore.Images.Thumbnails.IMAGE_ID);
        // Get the column index of the Thumbnails Image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);


        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        sdcardImages.setAdapter(new ImageAdapter(this));

        // Set up a click listener
      sdcardImages.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                // Get the data location of the image
                String[] projection = {MediaStore.Images.Media.DATA};
                cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        projection, // Which columns to return
                        null,       // Return all rows
                        null,
                        null);
                columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToPosition(position);
                // Get image filename
                 String imagePath = cursor.getString(columnIndex);
                 text.setText(imagePath);
                 Intent i = new Intent(getBaseContext(), preview.class);
                 i.putExtra("Value1", imagePath);
                         startActivity(i);

                }
        });
    }

    /**
     * Adapter for our image files.
     */
    private class ImageAdapter extends BaseAdapter {

        private Context context;

        public ImageAdapter(Context localContext) {
            context = localContext;
        }

        public int getCount() {
            return cursor.getCount();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) 
        {
            ImageView picturesView;
            if (convertView == null) 
            {
                picturesView = new ImageView(context);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // Set the content of the image based on the provided URI
                picturesView.setImageURI(Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
                picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                picturesView.setPadding(8, 8, 8, 8);
                picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
            }
            else 
            {
                picturesView = (ImageView)convertView;
            }
            return picturesView;
        }
    }
 }

Now when I use the path obtained from the above code to open an image in imageView it gives me null pointer exception my code for opening image in another activity using the path obtained is as follow:

package com.example.sdcardimage;

import java.io.BufferedInputStream;
import java.io.FileInputStream;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class preview extends Activity
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        ImageView picture = (ImageView)findViewById(R.id.imagepath);
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }
        String value1 = extras.getString("Value1");
        if (value1 != null)
        {
        TextView text = (TextView)findViewById(R.id.text);
        text.setText(value1);
        FileInputStream in;
        BufferedInputStream buf;    
    try{ 
        in = new FileInputStream(value1);
        buf = new BufferedInputStream(in);
        Bitmap bMap = BitmapFactory.decodeStream(buf);
        Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 150, 100, true);
        picture.setImageBitmap(bMapScaled);
        if (in != null) 
        {
            in.close();
           }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            Log.e("Error reading file", e.toString());
        }
       }
    }
}

Why am I getting null pointer exception and not able to display image in imageView?

My log is as follow

11-15 13:59:14.764: ERROR/AndroidRuntime(823): Uncaught handler: thread main exiting due to uncaught exception
11-15 13:59:14.814: ERROR/AndroidRuntime(823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sdcardimage/com.example.sdcardimage.preview}: java.lang.NullPointerException
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.os.Looper.loop(Looper.java:123)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.app.ActivityThread.main(ActivityThread.java:3948)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at java.lang.reflect.Method.invoke(Method.java:521)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at dalvik.system.NativeStart.main(Native Method)
11-15 13:59:14.814: ERROR/AndroidRuntime(823): Caused by: java.lang.NullPointerException
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at com.example.sdcardimage.preview.onCreate(preview.java:32)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
11-15 13:59:14.814: ERROR/AndroidRuntime(823):     ... 11 more

Update

I got the output as what I was looking for, so I am posting my modified code of second activity as follow, so that it will help others who are looking for the same thing:

package com.example.sdcardimage;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;


public class preview extends Activity
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String filename = extras.getString("Value1");
        FileInputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = new FileInputStream(new File(filename));
            bis = new BufferedInputStream(is);
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            ImageView image = (ImageView)findViewById(R.id.preview);
            image.setImageBitmap(bitmap);    
            } 
        catch (Exception e) {
            //Try to recover
        }
        finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (is != null) {
                    is.close();
                }    
            } catch (Exception e) {
            }
        }   
    }
}
  • 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-26T19:22:51+00:00Added an answer on May 26, 2026 at 7:22 pm

    use this code

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String filename = extras.getString("filename");
    Bitmap originalBitmap = BitmapFactory.decodeFile(filename);
    ImageView myimage = (ImageView) findViewById(R.id.ImageView01);
    myimage.setImageBitmap(originalBitmap);
    

    final solution

    Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String filename = extras.getString("Value1");
        FileInputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = new FileInputStream(new File(filename));
            bis = new BufferedInputStream(is);
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            ImageView image = (ImageView)findViewById(R.id.preview);
            image.setImageBitmap(bitmap);    
            } 
        catch (Exception e) {
            //Try to recover
        }
        finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (is != null) {
                    is.close();
                }    
            } catch (Exception e) {
            }
        }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to open a document using its default application in
I used to be able to open a new buffer in Emacs quickly using
I am using an XMLHttpRequest to fetch an image from a server (run locally
I'm using django-imagekit to get the image urls from a model called Avatar like
I want to be able to save a bitmap image with text from a
The following code transfers an image that is created on the fly from a
I have upload the video on facebook using following code Help From : https://github.com/zoul/facebook-ios-sdk/
I'd like to create an image using PIL and be able to email it
I'm able to get a bunch of image URLs using Amazon SimpleDB. I'm trying
i am able to open gallery for choose image and selected image set as

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.