Cargando imagen desde archivo en vista de lista; Androide

I have a tab view which has a listview in one of these tabs. This listview displays the name of files which contain images. When a name is clicked, I would like for it to display the image. I have tried numerous techniques but still keep running into errors and I have no idea what to do. If you could provide any help or information it will be greatly appreciated.

Aquí está el código:

package com.example.classname;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RecordsActivityTwo extends ListActivity {

private static String f2;
private static String fileNames[];
final File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/classname/Camera");
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    //ListDir(root);
    if (root.isDirectory()){
        //lists the file names into an array
        fileNames = root.list();


        //loop used to remove the jpg suffix on file names
        int cntr = 0;
        while (cntr <= (fileNames.length - 1)) {
            fileNames[cntr] = fileNames[cntr].replace(".jpg", "");
            cntr = cntr + 1;
        }

    } else {
        Toast.makeText(this, "Did not get into the directory", Toast.LENGTH_LONG).show();
    }

    setListAdapter(new ArrayAdapter<String>(this, R.layout.recordsactivitytwo, fileNames));






     final ListView lv = getListView();
     lv.setTextFilterEnabled(true);
     lv.setClickable(true);

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){

         Object o = lv.getItemAtPosition(position);
         f2 = o.toString() + ".jpg";
         Toast.makeText(getApplicationContext(), f2, Toast.LENGTH_LONG);
         //now write the code for loading the file
         //object o should be the file, so i need to load object o
         //the following code deals with opening the clicked file and displaying it
         imageViewMethod(f2);
         //Intent i = new Intent(getApplicationContext(), ImageViewActivity.class);
         //i.putExtra("text", f2);
         //startActivity(i);
         //ImageView iv = (ImageView)findViewById(R.id.imageviewreader);
         //iv.setImageBitmap(BitmapFactory.decodeFile(f2)); //displays the image from the file to the image view
     }
     });


}

public void imageViewMethod(String file){
    Logger log = Logger.getLogger("com.example.classname");

    try {
    File fileName = new File(root, file);
    String dirFileName = fileName.toString();
    Toast.makeText(getApplicationContext(), dirFileName, Toast.LENGTH_LONG).show();
    ImageView iv = (ImageView)findViewById(R.id.imageviewreader);
    iv.setImageBitmap(BitmapFactory.decodeFile(dirFileName));
    super.setContentView(iv);
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Hit Exception", Toast.LENGTH_LONG).show();
        log.log(Level.SEVERE, "uncaught exception", e);
    }
}

}

Y aquí está la salida logcat:

06-30 15:59:17.406: E/com.example.ufohunter(569): uncaught exception
06-30 15:59:17.406: E/com.example.ufohunter(569): java.lang.NullPointerException
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.example.ufohunter.RecordsActivityTwo.imageViewMethod(RecordsActivityTwo.java:93)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.example.ufohunter.RecordsActivityTwo$1.onItemClick(RecordsActivityTwo.java:73)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.widget.ListView.performItemClick(ListView.java:3513)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.os.Handler.handleCallback(Handler.java:587)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.os.Looper.loop(Looper.java:130)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.app.ActivityThread.main(ActivityThread.java:3683)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at java.lang.reflect.Method.invokeNative(Native Method)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at java.lang.reflect.Method.invoke(Method.java:507)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at dalvik.system.NativeStart.main(Native Method)

preguntado el 30 de junio de 12 a las 17:06

Which variable is null? Watch your variables in the imageViewMethod() method to track down which is the culprit. -

I have found out that the ImageView variable 'iv' is null. I don't understand how though, because I have created an imageview xml file to correspond with it and I also set the image to the image availabe in the file. Do you have any ideas? -

1 Respuestas

try to debug in order to see in which phase something went wrong . it seem you get a null pointer exception .

Respondido el 30 de junio de 12 a las 17:06

Thanks for your help! I went through the debugger and I found out that the ImageView variable 'iv' is null. However, this leaves me with another dilemma, because I had created an imageview xml file with an id imageviewreader and I had set the imageview to display the file. If you could provide any additional help I will be grateful. - cristina

you should try to read this tutorial about ListActivity : mkyong.com/android/android-listview-example .some mistakes i can find in your code: if you extend from ListActivity , you are not supposed to call "setContentView" . in the onItemClick , you are supposed to use the parameter "View arg1" , which is the one that the user has pressed on , and if it contains other views , you need to find them inside it and not inside the root of your activity . another tip : watch the video about listView : youtube.com/watch?v=wDBM6wVEO70 - desarrollador de Android

Thanks a lot! I found the article and video you sent me to be very useful - cristina

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.