将路径中的图像显示到ListView

时间:2015-03-17 03:22:11

标签: android listview

我能够将图像存储为路径。现在我可以检索并将其显示到imageview。但是,每当我尝试将图像显示为列表视图时,它都不起作用。请看下面的代码: 任何回复都将不胜感激。

 ImageView targetImage = (ImageView) findViewById(R.id.stud_list_image); 


 private void populateFields()  {

       Cursor data = studentsDbAdapter.queuePic();
       if (data.moveToFirst()) {
           cursorAdapter = new SimpleCursorAdapter(this, R.layout.stud_row,  data, new String[] {StudentsDbAdapter.KEY_ImagePathID, StudentsDbAdapter.KEY_ImagePath}, new int[] {R.id.textstud,R.id.stud_list_image});
           SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {
               public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                   if(view != null && view.getId() != R.id.stud_list_image) {
                       return false;
                   }
               String username = cursor.getString(cursor.getColumnIndex(StudentsDbAdapter.KEY_ImagePathID));
               String path =  loginDataBaseAdapter.getImapath(username);
               File image = new  File(path); 
               ((ImageView)view).setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath())); 
        ;

             return true;
               }
           };
           int index = data.getColumnIndex(StudentsDbAdapter.KEY_ImagePath);
           //Log.d(Constants.TAG, "" + index);
           cursorAdapter.setViewBinder(viewBinder);
           viewBinder.setViewValue(targetImage, data, index);
           listContent.setAdapter(cursorAdapter);
       }

        } 

更新了我的问题,这里是logcat:

03-17 12:08:30.543: ERROR/AndroidRuntime(30551): FATAL EXCEPTION: main
03-17 12:08:30.543: ERROR/AndroidRuntime(30551): java.lang.RuntimeException: Unable to resume activity {com.csu.eclassrecord/com.csu.eclassrecord.Students_Show}: java.lang.NullPointerException
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2899)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2928)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.os.Looper.loop(Looper.java:153)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.ActivityThread.main(ActivityThread.java:5297)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at java.lang.reflect.Method.invoke(Method.java:511)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at dalvik.system.NativeStart.main(Native Method)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551): Caused by: java.lang.NullPointerException
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at com.csu.eclassrecord.Students_Show$5.setViewValue(Students_Show.java:180)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at com.csu.eclassrecord.Students_Show.populateFields(Students_Show.java:187)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at com.csu.eclassrecord.Students_Show.onResume(Students_Show.java:148)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1190)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.Activity.performResume(Activity.java:5200)
03-17 12:08:30.543: ERROR/AndroidRuntime(30551):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2886)

更新

我尝试使用以下代码对其进行测试:

Cursor data = studentsDbAdapter.queuePic();
       if (data.moveToFirst()) {
           cursorAdapter = new SimpleCursorAdapter(this, R.layout.stud_row,  data, new String[] {StudentsDbAdapter.KEY_ImagePathID, StudentsDbAdapter.KEY_ImagePath}, new int[] {R.id.textstud,R.id.stud_list_image});
           SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {
               public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                   if(view != null && view.getId() != R.id.stud_list_image) {
                       return false;
                   }
               String username = cursor.getString(cursor.getColumnIndex(StudentsDbAdapter.KEY_ImagePathID));

              // String path =  studentsDbAdapter.getImapath("101-00017");
              // File image = new  File(path); 
               Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
               targetImage.setImageBitmap(imagenAndroid);
                   return true;     
               } 

           };
           int index = data.getColumnIndex(StudentsDbAdapter.KEY_ImagePath);
           //Log.d(Constants.TAG, "" + index);
           cursorAdapter.setViewBinder(viewBinder);
           viewBinder.setViewValue(targetImage, data, index);
           listContent.setAdapter(cu

rsorAdapter);

仍然会出错
viewBinder.setViewValue(targetImage, data, index);

2 个答案:

答案 0 :(得分:0)

替换

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
               if(view != null && view.getId() != R.id.stud_list_image) {
                   return false;
               }
           String username = cursor.getString(cursor.getColumnIndex(StudentsDbAdapter.KEY_ImagePathID));
           String path =  loginDataBaseAdapter.getImapath(username);
           File image = new  File(path); 
           ((ImageView)view).setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath())); 
    ;

         return true;
           }

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
    switch(view.getId()) { 
        case R.id.stud_list_image: 
            ((ImageView)view).setImageBitmap(BitmapFactory.decodeFile(this.getResources().getDrawable(R.drawable.ic_launcher)).getBitmap()));  
        return true;     
    }
}

顺便说一下,使用decodeFile方法时,只能显示小尺寸的图像。否则你应该自己调整图像大小。

答案 1 :(得分:0)

替换

((ImageView)view).setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath())); 

使用

targetImage.setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath()));