2016-09-30 41 views
0

我下載從互聯網上的圖像,這是我選擇它保存在手機上的路徑recyclerview圖片:問題用畢加索

ContextWrapper cw = new ContextWrapper(mContext); 
File directory = cw.getDir("imagesDB", Context.MODE_PRIVATE); 
OutputStream output = new FileOutputStream(new File(directory,"profile.jpg")); 

圖像已被下載我用這個來後獲取路徑:

String databasePath = mContext.getDir("", Context.MODE_PRIVATE).getAbsolutePath(); 
databasePath = databasePath + "imagesDB/profile.jpg"; 

然後在我的recyclerview適配器我用這個:

Picasso.with(context).load(databasePath).placeholder(R.mipmap.ic_launcher).into(holder.Photo); 

我總是得到顯示ic_launcher圖像而不是我下載的圖像。

我是否使用錯誤的路徑的圖像?

這是我用來下載圖像的代碼:

URL url = null; 
    try { 
     url = new URL("http://192.168.0.100/app/image/image1.jpg"); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    InputStream input = null; 
    try { 
     input = url.openStream(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     ContextWrapper cw = new ContextWrapper(mContext); 
     File directory = cw.getDir("imagesDB", Context.MODE_PRIVATE); 
     OutputStream output = new FileOutputStream(new File(directory,"profile.jpg")); 

     try { 
      byte[] buffer = new byte[10000]; 
      int bytesRead = 0; 
      while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { 
       output.write(buffer, 0, bytesRead); 
      } 
      String databasePath = mContext.getDir("", Context.MODE_PRIVATE).getAbsolutePath(); 
      Log.i("","Path1: "+ databasePath.toString()); 
      databasePath = databasePath + "imagesDB/profile.jpg"; 
      Log.i("","Path2: "+ databasePath.toString()); 
     } finally { 
      output.close(); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      input.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

首先在Log中打印_databasePath_並檢查其值。 – Piyush

+0

我認爲圖像需要從服務器下載並保存時間。並且在下載之前使用picaso附加圖像。 –

+0

你有沒有給你的清單寫讀權限?同時檢查你的變量是否有合適的路徑 – GrIsHu

回答

0

好了,問題是,我不能傳遞一個字符串給.load(),而不是我要創建這樣一個新的文件:

Picasso.with(context).load(new File(databasePath)).into(holder.artistPhoto); 

通過我發現這個職位的解決方式: Picasso Load image from filesystem

0

回收站適配器使用畢加索。其餘的代碼,如果任何人需要它,我會上傳。

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> { 
     private ArrayList<DogInfo> mArrayListInfo; 
     private Context context; 

      public RecyclerAdapter(ArrayList<DogInfo> mArrayListInfo,Context context) { 
       this.mArrayListInfo = mArrayListInfo; 
       this.context=context; 
      } 

      @Override 
      public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
       View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.lay_display,null); 

       return new MyViewHolder(view); 
      } 

      @Override 
      public void onBindViewHolder(MyViewHolder holder, int position) { 
       DogInfo dogInfo=mArrayListInfo.get(position); 
       holder.mTextView.setText(dogInfo.getmInfo()); 

       Picasso.with(context).load(dogInfo.getmImage()).into(holder.mImageView); 

      } 



      @Override 
      public int getItemCount() { 
       return mArrayListInfo.size(); 
      } 

      public class MyViewHolder extends RecyclerView.ViewHolder { 
       ImageView mImageView; 
       TextView mTextView; 
       public MyViewHolder(View itemView) { 
        super(itemView); 


        mImageView= (ImageView) itemView.findViewById(R.id.imgShow); 
        mTextView= (TextView) itemView.findViewById(R.id.txtShow); 

       } 
      } 
     }