2016-09-30 40 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); 

       } 
      } 
     }