java.net.MalformedURLException: Protocol not found

754 Views Asked by At

I am developing < news app and I need display images and text consistently in a Textview. This is the exact news I want to display in the app.

So I wrote following code:

public class NewsActivity extends AppCompatActivity implements FeedAdapter.OnItemClickListener, Html.ImageGetter
{

TextView textViewBody;

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

    textViewBody = findViewById(R.id.textview_newsBody);
    Intent intent = getIntent();
    String body = intent.getStringExtra(EXTRA_BODY);
    body = body.replace("&lt;", "<").replace("&gt;", ">");
    Spanned spanned = Html.fromHtml(body, this, null);
    textViewBody.setText(spanned);
}

@Override
public Drawable getDrawable(String source)
{
    LevelListDrawable drawable = new LevelListDrawable();
    Drawable empty = getResources().getDrawable(R.drawable.ic_arxiv);
    drawable.addLevel(0, 0, empty);
    drawable.setBounds(0,0,empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

    new LoadImage().execute(source, drawable);
    return drawable;
}

class LoadImage extends AsyncTask<Object, Void, Bitmap>
{
    private LevelListDrawable mDrawable;

    @Override
    protected Bitmap doInBackground(Object... params) {
        String source = (String) params[0];
        mDrawable = (LevelListDrawable) params[1];
        Log.d(TAG, "doInBackground " + source);
        try {
            InputStream is = new URL(source).openStream();
            return BitmapFactory.decodeStream(is);
        }  catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) 
    {
        Log.d(TAG, "onPostExecute drawable " + mDrawable);
        Log.d(TAG, "onPostExecute bitmap " + bitmap);
        if (bitmap != null) {
            BitmapDrawable d = new BitmapDrawable(getApplicationContext().getResources(), bitmap);
            mDrawable.addLevel(1, 1, d);
            mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            mDrawable.setLevel(1);
            // i don't know yet a better way to refresh TextView
            // mTv.invalidate() doesn't work as expected
            CharSequence t = textViewBody.getText();
            textViewBody.setText(t);
            textViewBody.refreshDrawableState();
        }
    }
}

I am getting a MalformedURLException for every image when trying to open this news:

W/System.err: java.net.MalformedURLException: Protocol not found: "https://metbuat.az/uploads/295/7f54088035-img6105.jpg"

If I copy-paste the URL to the browser it is OK and the protocol already provided.

Why am I still getting this exception?

2

There are 2 best solutions below

0
On

Hence change the line :

InputStream is = new URL(source).openStream();

like

InputStream is = new URL(source.replace("&quot;", "\"")).openStream();
0
On

Used glide esay to show image add below dependecy into app level gradle file.

implementation 'com.github.bumptech.glide:glide:4.7.1'

then make code like this..

    // if activity then pass only this and fragment then pass getActivity()
    Glide.with(this)
            .load("https://metbuat.az/uploads/295/7f54088035-img6105.jpg")
            .into(imageview);