【问题标题】:Unable to add image in a istview from the database无法在数据库中的 istview 中添加图像
【发布时间】:2016-06-17 17:29:27
【问题描述】:

为了在列表视图中显示图像,我使用了 picasso,但出现了这个错误

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。 我已经使用此代码显示了图像,但是当我尝试将其与列表视图一起使用时,它不起作用

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 截图

enter image description here

【问题讨论】:

  • 这是一个FileNotFoundException。这意味着http://aaaaa.com/PHP/images/image1.jpg 没有任何内容。 “http://aaaaa.com/PHP/images/”网址中没有image1.jpg
  • 此网址中有 2 张图片。我已经在其他活动中展示了它们
  • 也许吧,但是这两个图像的名称不是您在此行中指定的“image1.jpg”:Picasso.with(getActivity()) .load("http://aaaaa.com/PHP/images/"+image1+".jpg") .resize(50, 50) .into(im); 找出这些图像的确切文件名。
  • 名称正确。我用 FileZilla 截图更新了我的帖子
  • 是的,亲爱的。我真的很抱歉之前的肤浅一瞥。现在我更仔细地查看了您的代码,我有几个问题。你为什么这样做:final ImageView im =new ImageView (getActivity()); Picasso.with(getActivity()) .load("http://aaaaa.com/PHP/images/"+image1+".jpg") .resize(50, 50) .into(im);,你为什么将HashMaps 的ArrayList 传递给你的适配器,你能告诉我你的SimpleAdapter 的代码吗?

标签: android listview


【解决方案1】:

真的很抱歉回复晚了。 resolveUri failed on bad bitmap uri 通常在系统尝试从应用程序的资源目录中提取可绘制位图并且那里没有图像时出现。由于您使用的是SimpleAdapter 的默认实现,因此您不能使用图像。 SimpleAdapter 仅支持 TextViews。因此,您需要执行以下操作:

创建一个名为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);
}

【讨论】:

  • 感谢它工作得很好,因为我知道我替换了 LayoutInflater inflater = context.getLayoutInflater(); by LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);因为我有一个错误
  • 如何更改此方法,因为适配器出现错误 public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { // 当用户更改文本时 ((SimpleAdapter) ResultFragment .this.adapter).getFilter().filter(cs); }
  • 感谢您告诉我它有效。还请将我的回答标记为“已接受”,以便阅读这篇文章的人知道您与毕加索的问题已得到解决。如果你告诉我你想用((SimpleAdapter) ResultFragment.this.adapter).getFilter().filter(cs); 实现什么,我也许也能帮你。 :)
  • 至于你的过滤,你也可以通过this post在你的适配器类中实现一个自定义过滤器。如果您还需要任何进一步的帮助,请告诉我。 :)
  • 谢谢 如何添加自定义过滤器?我编辑了我的帖子。我在 CustomArrayAdapter 中添加了 getFilter) 方法,但是我可以在 if() if(data matches your filter criteria) { filterResultsData.add(data); 中添加什么如何在我的 inputSearch.addTextChangedListener(new TextWatcher() 方法中使用它?
【解决方案2】:

/BitmapFactory:无法解码流: java.io.FileNotFoundException:

这意味着您提供的图片网址不包含任何图片。请查收:http://aaaaa.com/PHP/images/"+image1+".jpg 对于特定文件夹中的image1.jpg

编辑 如果您在相同的 url 下有图像,请尝试将 url 更改为:

"http://aaaaa.com/PHP/images/"+"image1.jpg"

【讨论】:

  • 此网址中有 2 张图片(image1.jpg 和 image2.jpg)。我已经在其他活动中展示了它们
  • @LaraFab 具有相同的网址?
  • 我得到了同样的错误,为什么我在带有静态 url 的错误中得到了 image2?E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image1: open failed: ENOENT (No such文件或目录)06-14 12:57:47.596 2545-2545/com.example.dell.app2 I/System.out:resolveUri 在错误的位图 uri 上失败:image1 06-14 12:57:47.600 2545-2545/com .example.dell.app2 E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/image2:打开失败:ENOENT(没有这样的文件或目录)06-14 12:57:47.600 2545-2545/com.example .dell.app2 I/System.out:resolveUri 在错误的位图 uri 上失败:image2
  • 有重复 3 次 image1 image2 image1 image2 image1 image2 如我的第一篇文章所示
猜你喜欢
  • 1970-01-01
  • 2010-10-12
  • 2013-08-03
  • 2021-08-31
  • 2012-07-10
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多