在我的Android应用程序中,我想使用Picasso在GridView中显示图像。所有图像都在我的扩展(obb)文件中。我可以通过InputStream从扩展文件中获取drawable,但我不知道如何使用Picasso加载它们。到目前为止我的代码:
public class Tab3 extends Fragment {
public com.android.vending.expansion.zipfile.ZipResourceFile expansionFile;
public String[] photoID;
public Integer photoNumberOf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_3, container, false);
GridView gridView = (GridView) view.findViewById(R.id.photogridview);
gridView.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getActivity().getSharedPreferences("FILTER_PREFS", Context.MODE_PRIVATE);
String photoString = settings.getString("DESC_MainPhoto", null);
photoID = photoString.split(",");
photoNumberOf = photoID.length;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return photoID.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
try {
expansionFile = com.android.vending.expansion.zipfile.APKExpansionSupport.getAPKExpansionZipFile(getActivity(), 2, 0);
InputStream fileStream = expansionFile.getInputStream(photoID[position] + ".jpg");
Drawable drawable = Drawable.createFromStream(fileStream, null);
} catch (IOException e) {
}
Picasso.with(getContext())
.load(???????????????????????????????)
.resize(width, 0)
.into(imageView);
return imageView;
}
}
答案 0 :(得分:0)
Picasso提供了3种加载图像的方法。
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(new File(...)).into(imageView3);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
当您知道文件位置时,可以使用第二种方法。
答案 1 :(得分:0)
使用毕加索的文件方法
Picasso.with(getContext())
.load(new File(path))
.resize(width, 0)
.into(imageView);