在列表视图中显示图像我使用了毕加索,但是我收到了这个错误
06-14 11:07:10.780 1197-1197/com.example.dell.app2 E/value:: [{"Reference":"QNPD2016","Nom":"ERK","Description":"done","image":"image1"},{"Reference":"TTGH25361","Nom":"PSD","Description":"usage","image":"image2"}]
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
06-14 11:07:10.800 1197-1197/com.example.dell.app2 W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
06-14 11:07:10.824 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image1: open failed: ENOENT (No such file or directory)
06-14 11:07:10.824 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image1
06-14 11:07:10.828 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image2: open failed: ENOENT (No such file or directory)
06-14 11:07:10.832 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image2
06-14 11:07:10.840 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image1: open failed: ENOENT (No such file or directory)
06-14 11:07:10.844 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image1
06-14 11:07:10.844 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image2: open failed: ENOENT (No such file or directory)
06-14 11:07:10.848 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image2
06-14 11:07:10.848 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image1: open failed: ENOENT (No such file or directory)
06-14 11:07:10.852 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image1
06-14 11:07:10.884 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image2: open failed: ENOENT (No such file or directory)
06-14 11:07:10.884 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image2
如何更正此错误 这是我的代码
我使用了json和picasso。 我已经使用此代码显示了一个图像,但是当我尝试将它与listview一起使用时,它无法正常工作
public class ResultFragment extends Fragment implements ListView.OnItemClickListener {
private String JSON_STRING;
private ImageButton btn1;
ListAdapter adapter;
public static final String URL = "http://aaaaa.com/PHP/getAllRes.php";
//public static final String TAG_JSON_ARRAY = "result";
// List view
private ListView listView;
// Search EditText
EditText inputSearch;
private ImageView imageView1;
LinearLayout layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_produits, container, false);
listView = (ListView) rootview.findViewById(R.id.listView);
inputSearch = (EditText) rootview.findViewById(R.id.inputSearch);
listView.setOnItemClickListener(this);
imageView1= (ImageView)rootview.findViewById(R.id.imageView1);
layout=(LinearLayout) rootview.findViewById(R.id.layout);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
((SimpleAdapter) ProduitsFragment.this.adapter).getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
getJSON();
return rootview;
}
private void showProduct(String json){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
String image1 = jo.getString("image");
final ImageView im =new ImageView (getActivity());
Picasso.with(getActivity())
.load("http://aaaaa.com/PHP/images/"+image1+".jpg")
.resize(50, 50)
.into(im);
//layout.addView(im);
HashMap<String,String> product= new HashMap<>();
product.put("name",name);
product.put("ref",ref);
product.put("image1",image1);
list.add(product);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row,
new String[]{"image1","ref","name"},
new int[]{R.id.imageView1, R.id.nom, R.id.email2});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
Log.e("value: ", JSON_STRING);
showProduct(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewProduit.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("ref").toString();
intent.putExtra("ref", tid);
startActivity(intent);
}
}
编辑 CustomArrayAdapter .java
public class CustomArrayAdapter extends ArrayAdapter<Product> {
private Context context;
private ArrayList<Product> productList;
private ArrayList<HashMap<String, String>> filteredData;
public CustomArrayAdapter(Context context, ArrayList<Product> productList) {
super(context, R.layout.listrow2, productList);
this.context = context;
this.productList = productList;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.listrow2, parent, false);
TextView nom = (TextView) rowView.findViewById(R.id.nom);
TextView email2 = (TextView) rowView.findViewById(R.id.email2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
nom.setText(productList.get(position).getName()); //make sure nom is for name and email2 is for ref. This is just my guesswork.
email2.setText(productList.get(position).getRef());
Picasso.with(context).load(productList.get(position).getImageUrl()).into(imageView);
return rowView;
}
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults results = new FilterResults();
//If there's nothing to filter on, return the original data for your list
if(charSequence == null || charSequence.length() == 0)
{
results.values = productList;
results.count = productList.size();
}
else
{
ArrayList<HashMap<String,String>> filterResultsData = new ArrayList<HashMap<String,String>>();
for(HashMap<String,String> data : productList)
{
//In this loop, you'll filter through originalData and compare each item to charSequence.
//If you find a match, add it to your new ArrayList
//I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
if(data matches your filter criteria)
{
filterResultsData.add(data);
}
}
results.values = filterResultsData;
results.count = filterResultsData.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
filteredData = (ArrayList<HashMap<String,String>>)filterResults.values;
notifyDataSetChanged();
}
};
}
}
我希望获得这样的列表视图 enter image description here
这是我的filezilla截图
答案 0 :(得分:2)
对于迟到的回复真的很抱歉。如果系统试图从应用程序的资源目录中提取可绘制的位图并且那里没有图像,则通常会出现resolveUri failed on bad bitmap uri
。由于您使用的是SimpleAdapter
的默认实现,因此无法使用图像。 SimpleAdapter仅支持TextView。因此,您需要执行以下操作:
创建一个名为Product
的普通旧Java类,如下所示:
public class Product {
String name;
String ref;
String imageUrl;
public String getName() {
return name;
}
public String getRef() {
return ref;
}
public String getImageUrl() {
return imageUrl;
}
public void setName(String name) {
this.name = name;
}
public void setRef(String ref) {
this.ref = ref;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
创建自定义ArrayAdapter
:
public class CustomArrayAdapter extends ArrayAdapter<Product>{
private Context context;
private ArrayList<Product> productList;
public CustomArrayAdapter(Context context, ArrayList<Product> productList) {
super(context, R.layout.listrow, productList);
this.context = context;
this.productList = productList;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.listrow, parent, false);
TextView nom = (TextView) rowView.findViewById(R.id.nom);
TextView email2 = (TextView) rowView.findViewById(R.id.email2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
nom.setText(productList.get(position).getName()); //make sure nom is for name and email2 is for ref. This is just my guesswork.
email2.setText(productList.get(position).getRef());
Picasso.with(context).load(productList.get(position).getImageUrl()).into(imageView);
return rowView;
}
}
现在,修改您的showProduct
:
private void showProduct(String json){
JSONObject jsonObject = null;
ArrayList<Product> productList = new ArrayList<>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
String image1 = "http://aaaaa.com/PHP/images/"+jo.getString("image")+".jpg";
Product product = new Product();
product.setName(name);
product.setRef(ref);
product.setImageUrl(image1);
productList.add(product);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new CustomArrayAdapter(getContext(), productList);
listView.setAdapter(adapter);
}
如果有帮助,请告诉我。
修改强>
对onItemClick
进行这些更改:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewProduit.class);
Product product = (Product) parent.getItemAtPosition(position);
String tid = product.getRef();
intent.putExtra("ref", tid);
startActivity(intent);
}
答案 1 :(得分:0)
/ BitmapFactory:无法解码流: java.io.FileNotFoundException:
这意味着您提供的图片网址不包含任何图片。请检查:http://aaaaa.com/PHP/images/"+image1+".jpg
对于特定文件夹中的image1.jpg
。
修改强> 如果您在同一网址下有图片,请尝试将网址更改为:
"http://aaaaa.com/PHP/images/"+"image1.jpg"