2016-03-01 154 views
1

我想加載文本和圖像(存儲爲圖像路徑)從MySQL數據庫到列表視圖。我能夠獲取文本,但圖像未加載。通過大量的教程和SO問題,但無法解決它。圖像不加載從MySQL存儲爲圖像路徑

我與位圖試圖像下面

Bitmap bmImg = BitmapFactory.decodeFile("path of your img1"); 
imageView.setImageBitmap(bmImg); 

不工作。試圖與文件文件= ...也但也沒有工作。以下是帶有最新更改的完整代碼,但會引發網絡異常。

public class MovieAdapter extends BaseAdapter{ 

private final sharedcollect sc; 
private final ArrayList<Mdetails> mdetails; 

public MovieAdapter(sharedcollect sc, ArrayList<Mdetails> mdetails) { 
    this.sc = sc; 
    this.mdetails = mdetails; 
} 

@Override 
public int getCount() { 
    return mdetails.size(); 
} 

@Override 
public Object getItem(int position) { 
    return mdetails.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    Mdetails md = mdetails.get(position); 
    String moviename = md.moviename; 
    String year = md.year; 
    String imdbrating = md.irating; 
    String rottenrating = md.rating; 
    String genere = md.genere; 
    String Image = md.Image; 
    Bitmap bimage; 


    View view= LayoutInflater.from(sc).inflate(R.layout.custlayout,null); 
    TextView mname = (TextView)view.findViewById(R.id.moviename); 
    TextView myear = (TextView)view.findViewById(R.id.year); 
    TextView iname = (TextView)view.findViewById(R.id.irating); 
    TextView rname = (TextView)view.findViewById(R.id.rating); 
    TextView gname = (TextView)view.findViewById(R.id.genere); 

    bimage = getBitmapFromURL(Image); 
    ImageView iview= (ImageView)view.findViewById(R.id.imageView); 

    mname.setText(moviename); 
    myear.setText(" "+year); 
    iname.setText(" "+irating ); 
    rname.setText(" "+rating); 
    gname.setText("  " + genere); 
    iview.setImageBitmap(bimage); 

    return view; 
} 

public static Bitmap getBitmapFromURL(String image) { 

    try { 
     URL url = new URL(image); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
    } 
} 

這段代碼有什麼問題?

的logcat:

FATAL EXCEPTION: main 
    Process: com.moviefilter.as, PID: 20319 
    android.os.NetworkOnMainThreadException 
     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 
     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110) 
     at libcore.io.IoBridge.connectErrno(IoBridge.java:137) 
     at libcore.io.IoBridge.connect(IoBridge.java:122) 
     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 
     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456) 
     at java.net.Socket.connect(Socket.java:882) 
     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174) 
     at com.android.okhttp.Connection.connect(Connection.java:152) 
     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276) 
     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211) 
     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382) 
     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106) 
     at com.moviefilter.allmovieratings.MovieAdapter.getBitmapFromURL(MovieAdapter.java:85) 
     at com.moviefilter.allmovieratings.MovieAdapter.getView(MovieAdapter.java:66) 
     at android.widget.AbsListView.obtainView(AbsListView.java:2347) 
     at android.widget.ListView.measureHeightOfChildren(ListView.java:1270) 
     at android.widget.ListView.onMeasure(ListView.java:1182) 
     at android.view.View.measure(View.java:17547) 
     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727) 
     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463) 
     at android.view.View.measure(View.java:17547) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 
     at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124) 
     at android.view.View.measure(View.java:17547) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) 
     at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393) 
     at android.view.View.measure(View.java:17547) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 
     at android.view.View.measure(View.java:17547) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) 
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
     at android.view.View.measure(View.java:17547) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 
     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615) 
     at android.view.View.measure(View.java:17547) 
     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015) 
     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173) 
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379) 
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) 
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885) 
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
     at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
     at android.view.Choreographer.doFrame(Choreographer.java:550) 
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5254) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
+0

你只是存儲在數據庫中的圖像路徑??? – Nils

+0

是,僅圖像路徑。 – user2269164

+0

以及來自網絡的異常,所以你檢查你的互聯網連接..? – Nils

回答

0

確保您已經添加了互聯網許可清單文件。對於圖片,我會推薦使用流行的Picasso庫。您可以使用單個語句加載圖像,如Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);它自動處理圖像加載和緩存。

編輯:當您嘗試在主線程上執行網絡操作時,會導致此異常。您需要使用類似AsyncTask的網絡操作。您可以使用this示例使用AsyncTask進行加載。如果你想簡單的解決這個問題,可以使用上面提到的庫。

+0

嗎? – user2269164

+0

@ user2269164是的。 –

+0

@ user2269164我也更新了我的AsyncTask示例。 –

0

嘗試this.It將解決您的錯誤

 Thread thread = new Thread(new Runnable(){ 
       @Override 
       public void run() { 
        try {                  

          Bitmap mIcon11 = null;        
          // create instance of InputStream and pass URL 
          InputStream in = new java.net.URL(imagelink).openStream(); 
          // decode stream and initialize bitmap 
          mIcon11 = BitmapFactory.decodeStream(in); 

          in.close(); 

         } 


         //Your code goes here 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
+0

@ user2269164你必須在一個線程中使用它來避免異常 –

相關問題