简体   繁体   English

无法将图片从url添加到我的运行时在android中创建的textView

[英]unable to add picture from url to my runtime created textView in android

I have two activities in my android app in opening activity I'm pasting a url into edit text and on clicking fetch button it extracts the description and title of given URL and opens new activity with URL, title and description added to edittext of 2nd activity and on clicking button I want to create a new textview with URL info with related picture and add it to layout like messages are adds in WhatsApp app on clicking send button. 我在Android应用程序中有两个活动,在打开活动中,我将一个URL粘贴到编辑文本中,然后单击获取按钮,它提取给定URL的描述和标题,并打开新活动,并在第二个活动的edittext中添加URL,标题和描述然后单击按钮,我想创建一个带有相关图片的URL信息的新textview并将其添加到布局中,就像在单击“发送”按钮时在WhatsApp应用中添加消息一样。 I'm successful in creating textview with url info but the picture is not coming. 我成功创建了带有URL信息的textview,但是图片没有显示。 here is my 2nd activity code..... note: url of the image I've given is not runtime generated I gave a constant image src for debugging purpose. 这是我的第二个活动代码.....注意:我给的图像的url不是运行时生成的,我给出了一个恒定的图像src用于调试。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wall);
    initialize();
    Bundle gotBasket = getIntent().getExtras();
    baseurl = gotBasket.getString("url");
    title = gotBasket.getString("title");
    description = gotBasket.getString("des");
    imgsrc = gotBasket.getString("img");
    info = baseurl +"\n" + title + "\n" + description;
    content.setText(info);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void onClick(View v){
    String cntnt = content.getText().toString();
    wall.addView(createNewTextView(cntnt));
    content.setText("");
    scroll.fullScroll(ScrollView.FOCUS_DOWN);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textView = new TextView(this);
    int id = View.generateViewId();
    textView.setId(id);
    lparams.gravity = Gravity.RIGHT;
    lparams.setMargins(200,10,10,0);
    textView.setLayoutParams(lparams);
    textView.setTextSize(30);
    textView.setBackgroundColor(Color.WHITE);
    textView.setText(text);
    Linkify.addLinks(textView, Linkify.WEB_URLS);
    GradientDrawable gd = new GradientDrawable();
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setColor(Color.WHITE);
    gd.setStroke(2, Color.BLACK);
    gd.setCornerRadius(20.0f);
    textView.setBackground(gd);
    textView.setPadding(10,10,10,10);
    textView.setGravity(Gravity.LEFT);
    new FetchImageAsyncTask().execute();
    return textView;
}

private void initialize() {
    wall = (LinearLayout) findViewById(R.id.wall);
    content = (EditText) findViewById(R.id.content);
    post = (ImageButton) findViewById(R.id.post);
    scroll = (ScrollView) findViewById(R.id.scroll);
    post.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
public class FetchImageAsyncTask extends AsyncTask<Void, Void, Void> {

    Drawable d;
    @Override
    protected Void doInBackground(Void... params) {
        try {
            //imgsrc = "//upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Aishwarya_Rai_Cannes_2017.jpg/220px-Aishwarya_Rai_Cannes_2017.jpg";
            URL aURL = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Aishwarya_Rai_Cannes_2017.jpg/220px-Aishwarya_Rai_Cannes_2017.jpg");
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
    /* Buffered is always good for a performance plus. */
            BufferedInputStream bis = new BufferedInputStream(is);
    /* Decode url-data to a bitmap. */
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();

            d =new BitmapDrawable(bm);
            //image = Drawable.createFromStream((InputStream)new URL("https://en.wikipedia.org/wiki/File:Aishwarya_Rai_Cannes_2017.jpg").getContent(), "src");
        } catch (IOException e) {
            Log.e("DEBUGTAG", "Remote Image Exception", e);
        }

        return null;
    }

    protected void onPostExecute(Long result) {
        textView.setCompoundDrawables(null, null, null, d);
    }
}

Try this using Glide library you can set textview drawable: add this dependency in your gradle file 使用Glide库尝试一下,您可以设置textview可绘制:在gradle文件中添加此依赖项

compile 'com.github.bumptech.glide:glide:3.7.0'

Glide.with(this)
        .load(((FixturesListObject) object).getHomeIcon())
        .asBitmap()
        .into(new SimpleTarget<Bitmap>(100,100) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                        txtView.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(left.getResources(),resource), null, null);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM