0

I am splitting this off from my other thread here: Display image in popout window after button is clicked -- Android/Java

The old thread got very convoluted and confusing, and a bit off-topic, so I wanted to create another one that is clearer with more information.

I am trying to display an image in Android using an image file path that changes every time the app is run. I know there are ways to declare resources in the XML layout file, but since the picture I'd like to display is taken from the camera, I can't do it that way. I need to be able to display the image without hardcoding it.

The following is the code that I have:

photo_viewer.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pictureViewer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/viewImage" />

</RelativeLayout>

Java method to display image:

public void viewPhoto(String file){ 
    ImageView imageView = new ImageView(getApplicationContext());
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    Bitmap image = BitmapFactory.decodeFile(file);
    imageView.setImageBitmap(image);
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.pictureViewer);
    rl.addView(imageView, lp);
}

However, when I run the above code the application crashes, saying it has stopped working. "file" is a string denoting the directory where the image from the camera is stored, and I know this part is correct. I can use it (using a different method) to change the wallpaper. It's just displaying it that is causing me trouble. I have tried various methods to simply display the image (dialogs, "drawable"s, bitmap factory, etc) and had the same problem with all of them. I feel like it's something simple that I have missed, since I'm new to Android app development. I'm hoping one of you folks might be able to shed some light on this.

EDIT:

I have decided to try another route, but I am still getting a null pointer exception. Here is all my code:

main class, viewPhoto method:

/* View photo */
public void viewPhoto(String file){ 
    Intent imageView = new Intent(this, PhotoViewer.class);
    imageView.putExtra("directory", file);
    startActivity(imageView);
}

PhotoViewer class:

package com.androidproject;

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

public class PhotoViewer extends Activity {

    public String file;
    ImageView image;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.photo_viewer);

        //get file path of image
        Bundle extras = getIntent().getExtras();
        file = extras.getString("directory");

        Drawable drawable = Drawable.createFromPath(file);
        image.setImageDrawable(drawable);
    }
}

photo_viewer.xml (created using the Eclipse tool, so it's not just any xml file)

<?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/photo_viewer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".PhotoViewer" >

    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:contentDescription="@string/viewImage"/>

</RelativeLayout>

Project manifest:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidproject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.SET_WALLPAPER" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".PhotoViewer"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.androidproject.PhotoViewer" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The app continues to crash and tell me "unfortunately it stopped working". Here's the logcat of the error:

enter image description here

As always, help is appreciated!

Community
  • 1
  • 1
DerStrom8
  • 1,311
  • 2
  • 23
  • 45
  • 2
    Please post your logcat error output. I guess you are getting an OutOfMemory error. – Ken Wolf Nov 26 '13 at 17:07
  • 1
    Can't you just call `setImageBitmap()` on the `ImageView` you already have? Just use `findViewById(R.id.imageView)` to use the one you created in XML instead of creating a new one each time. – Geobits Nov 26 '13 at 17:11
  • 1
    You also probably shouldn't be using `getApplicationContext()` for creating views. – Geobits Nov 26 '13 at 17:13
  • @KenWolf I'll have that output for you in just a moment. Geobits, you're absolutely right about the ImageView. I'll change that as well. – DerStrom8 Nov 26 '13 at 17:20
  • Here's the logcat output. Apparently I'm getting a null pointer exception (I hadn't seen it before): http://oi43.tinypic.com/29vjrkx.jpg – DerStrom8 Nov 26 '13 at 17:27
  • Well, have a look at line 157 of Main.java - something there is `null`...and the code was not expecting that. – Ken Wolf Nov 26 '13 at 17:28
  • Indeed, apparently findViewById(R.id.picture_viewer) is returning null. photo_viewer.xml is a separate layout, I also have a main_layout which handles the first window with the "take photo" option and everything. It seems it doesn't like trying to look at a different layout file...? Again, I'm probably just doing it wrong. – DerStrom8 Nov 26 '13 at 17:41
  • It looks like I'm not using the second xml layout file properly. It is possible to create a second "window" using a second xml file but call it from the same java file, correct? – DerStrom8 Nov 26 '13 at 17:44
  • You can't just call an XML file - think of your app in terms of activities, fragments, views, and dialogs, and take it from there. Not sure exactly what you are trying to do but you could define everything in your main activity XML and just hide/show the elements you need by changing visibility. Alternatively you could "popup" a new layout as a dialog or fragment. – Ken Wolf Nov 26 '13 at 17:49
  • @KenWolf I think that's exactly what I was trying to do in the other thread--use a dialog "popup" to show the image. The problem I had when doing that was that the resource had to be defined in the XML file. I couldn't seem to figure out how to define it using the Java program. – DerStrom8 Nov 26 '13 at 20:39

1 Answers1

1

The null pointer exception is because

 Drawable drawable = Drawable.createFromPath(file);
 image.setImageDrawable(drawable);

is referencing an uninitiated variable (image) which is null

My best guess would be that to eliminate said error you'd want to initiate the variable

ImageView image = (ImageView)findViewById(R.id.imageView);
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • That is a good point. Unfortunately making that change didn't seem to help, it still returns with a dialog saying it stopped working. – DerStrom8 Nov 28 '13 at 02:54
  • You'd best post the amended logcat then – DigCamara Nov 28 '13 at 03:06
  • Oops, I'm sorry, I meant to do that. Here you go: http://oi44.tinypic.com/2r2ooky.jpg – DerStrom8 Nov 28 '13 at 03:25
  • The link you posted tells us you're still having a Null Pointer Exception, this time when you try to use findViewById on line 15. However, since you didn't refresh your code above, it's impossible for me to diagnose your problem. Hint: Edit your question above and/or mark this one as closed and post a new question with your new code and error or something like that. – DigCamara Nov 28 '13 at 04:50
  • Hmm, that's very strange. All I did was change ImageView image to ImageView image = (ImageView)findViewById(R.id.imageView), ran the program, and it still gave me the NPE. I just ran the program again and it seemed to work. Thank you so much for your help though, your suggestion was the one that solved it! – DerStrom8 Nov 28 '13 at 05:05