ANDROID:从解析到Android活动检索图像

时间:2017-02-22 07:08:24

标签: java android listview android-fragments parse-server

我创建了一个ListView。当我单击ListView的单个项目时,我调用一个活动来显示单个项目View。 TextView数据正在解析。但是当我试图通过解析来调用图像时它没有加载(图像在一个单独的位置在线托管)。如果有人可以请告诉如何在这里加载图像??

  

我的档案网址

url = new URL("file location " + statusObject.getString("image"));
  

已编辑的代码

   URL url = null;
                    try {
                        url = new URL("https://www.gravatar.com/avatar/db5bfe41c9d21d3a5b4e5b5f6a2d776d?s=32&d=identicon&r=PG&f=1");
                    } catch (MalformedURLException e1) {
                        e1.printStackTrace();
                    }
                    Bitmap bmp = null;
                    try {
                        bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    txtv2.setImageBitmap(bmp);
  

////////////////

单项视图

import android.content.*;
import android.net.*;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.*;
import android.view.View;
import android.widget.*;

import com.parse.*;
import com.parse.ParseException;

public class SingleItemView extends AppCompatActivity {

    String objectId;
    protected TextView txtv;
    protected TextView txtv1;
    protected ImageView txtv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_item_view);


        txtv =(TextView)findViewById(R.id.txt123);
        txtv1 =(TextView)findViewById(R.id.txt1234);
        txtv2 =(ImageView)findViewById(R.id.txt12345);
        Intent i =getIntent();

        objectId = i.getStringExtra("objectId");

        ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
        query.getInBackground(objectId, new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {

                    String username = object.getString("firstname");
                    txtv.setText(username);
                    String position = object.getString("position");
                    txtv1.setText(position);
                    String img_ = object.getString("image");
                    txtv2.setImageURI(Uri.parse(img_));

                } else {
                    // something went wrong
                }
            }
        });



    }

}

片段文件

import android.content.*;
import android.os.Bundle;
import android.support.v4.app.*;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;

public class Individuals extends android.support.v4.app.ListFragment
        implements FindCallback<ParseObject> {

    private List<ParseObject> mOrganization = new ArrayList<ParseObject>();
    SearchView sv;
    IndividualsAdaptor adaptor;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.individuals, container, false);
    }
    @Override
    public void onViewCreated(View view, Bundle b) {
        super.onViewCreated(view, b);
        sv = (SearchView) view.findViewById(R.id.ser1);
        adaptor = new IndividualsAdaptor(getActivity(), mOrganization);
        setListAdapter(adaptor);
        ParseQuery.getQuery("_User").findInBackground(this);

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String text) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String text) {
                adaptor.getFilter().filter(text);
                return true;
            }
        });
    }
    @Override
    public void done(List<ParseObject> scoreList, ParseException e) {
        if (e == null) {
            Log.d("score", "Retrieved " + scoreList.size() + " _User");
            mOrganization.clear();
            mOrganization.addAll(scoreList);
            ((IndividualsAdaptor) getListAdapter()).updateBackupList(mOrganization);
            ((IndividualsAdaptor) getListAdapter()).notifyDataSetChanged();
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }

   @Override
    public void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ParseObject prs = mOrganization.get(position);
        String objectId = prs.getObjectId();
        Intent i = new Intent(getActivity(), SingleItemView.class);
        i.putExtra("objectId", objectId);
        startActivity(i);

    }
}

2 个答案:

答案 0 :(得分:1)

<uses-permission android:name="android.permission.INTERNET" />中定义Manifest.xml

然后使用PicassoGilde并解析各自实例中的图片,并传递您要解析的图片网址。这些库将解析URL并在ImageView中返回图像。

其他类似问题: Load image from url

How to load an ImageView by URL in Android?

答案 1 :(得分:1)

  

最后这段代码在从答案中的链接编辑一些代码后工作。通过此代码,您可以   为每个对象ID调用动态图像。

 URL url = null;
                        try {
                            url = new URL("image url" + object.getString("image"));
                        } catch (MalformedURLException e1) {
                            e1.printStackTrace();
                        }
                        Bitmap bmp = null;
                        try {
                            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        txtv2.setImageBitmap(bmp);

                        // something went wrong