图像未从sqlite db字段显示

时间:2011-12-03 11:03:06

标签: android sqlite

我无法在我正在处理的应用上检索我的sd上的图像路径。 我尝试过使用setImageDrawable方法以及setImageURI方法 似乎在我的SD卡上显示图片。 picpath的数据库字段是一个字符串 只指向我SD卡上的文件的字段。我得到一个运行时标准运行时异常 堆栈跟踪中的错误。代码如下:

public void DisplayAllStudents(){

         //File f = new File("/sdcard/studentpics/gntamT.jpg");

         DBAdapter db = new DBAdapter(this);

         db.open();

         Cursor c = db.getAllStudents();

         if(c.moveToFirst()){

             do{

                 stuID = c.getString(c.getColumnIndex(db.KEY_ROWID));
                 stuName = c.getString(c.getColumnIndex(db.KEY_STUDENTNAME));
                 stuDOB = c.getString(c.getColumnIndex(db.KEY_DOB));
                 stuAddress1 = c.getString(c.getColumnIndex(db.KEY_ADDRESS1));
                 stuAddress2 = c.getString(c.getColumnIndex(db.KEY_ADDRESS2));
                 stuTown = c.getString(c.getColumnIndex(db.KEY_TOWN));
                 stuPostcode = c.getString(c.getColumnIndex(db.KEY_POSTCODE));
                 stuPhone = c.getString(c.getColumnIndex(db.KEY_PHONE));
                 stuPicPath = c.getString(c.getColumnIndex(db.KEY_PIC_PATH));

                 //add database values to ArrayList
                 results.add(" " + Html.fromHtml(stuName.toString() + "<br>" + stuDOB + "<br>" + stuAddress1 + "<br>" + stuAddress2 + "<br>" + stuTown + "<br>" + stuPostcode + "<br>" + stuPhone));

                 //add fields to our second array list so that we can
                 //pass ALL of the fields to the student details screen
                 dataList.add(stuID.toString() + "," + stuName.toString() + "," + stuDOB.toString() + "," + stuAddress1.toString() + "," + stuAddress2.toString() + "," + stuTown.toString() + "," + stuPhone.toString());


                 ImageView imgView = (ImageView) findViewById(R.id.studenticon);
                 imgView.setImageDrawable(Drawable.createFromPath(strPathToPic));


                 //display record
                 //DisplayStudent(c);
             }while(c.moveToNext());
         }
        //bind data to list view
         list.setAdapter(new ArrayAdapter<String>(StudentTrackerActivity.this,R.layout.datalayout,R.id.txtFname,results));



        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {

               try{


                String item = list.getItemAtPosition(position).toString();


                Intent intent = new Intent(StudentTrackerActivity.this,StudentDetails.class);

                //pass values from list view to student details 
                //screen 
                Bundle b = new Bundle(); 
                b.putString("stDetails", item);

                intent.putExtras(b);

                startActivity(intent);


               }catch(IllegalStateException ex){

                   ex.printStackTrace();
               }

                //Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();





            }
        });

         db.close();
    }

图片的字符串路径:

strPathToPic = dir.toString() + "/" + txtStudentName.getText().toString().trim() + ".jpg";

堆栈追踪:

Thread [<3> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2496  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2512   
    ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
    ActivityThread$H.handleMessage(Message) line: 1863  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4363    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 860  
    ZygoteInit.main(String[]) line: 618 
    NativeStart.main(String[]) line: not available [native method]  

2 个答案:

答案 0 :(得分:0)

您不是直接绘制图像,因此请使用此方法并绘制图像

    Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException 
{
    return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);
}

答案 1 :(得分:0)

Bitmap photobitmap = BitmapFactory.decodeFile(strPathToPic);
imageView.setImageBitmap(photobitmap);