1

I'm new to android and I'm trying to download images from url using Volley. I've already succeeded to display the image from some url using RequestQueue and ImageRequest. But now I'm trying to download the image to the device to some specific folder. By now i have a simple code that shows the image from url that I insert in TextView. My purpose now is to download and save the image from the inserted url.

I saw some examples that require ImageLoader.getInstance() and I don't understand why my ImageLoader doesn't have getInstance() method at all.

And maybe somebody have a better idea how to save the image from url to my android device to specific folder? Thanks! enter image description here

Java:

    public class MainActivity extends AppCompatActivity {

    private String url = "http://iokds.org/wp-content/uploads/2016/10/product-default-300x300.png";
    ImageView image;
    EditText insertedLink;
    Button submitBtn;
    RequestQueue mRequestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView)findViewById(R.id.ivImageView);
        insertedLink = (EditText) findViewById(R.id.insertLink);
        submitBtn = (Button) findViewById(R.id.btn);

        submitBtn.setOnClickListener(new SubmitButtonOnClickListener());
        showImage(url);
    }

    private void showImage(String newUrl){
        mRequestQueue = Volley.newRequestQueue(this.getApplicationContext());
        ImageRequest imageRequest = new ImageRequest(newUrl, new BitmapListener(), 0, 0, null, null, new MyErrorListener());
        mRequestQueue.add(imageRequest);
    }

    private class BitmapListener implements Response.Listener<Bitmap> {
        @Override
        public void onResponse(Bitmap response) {
            image.setImageBitmap(response);
        }
    }

    private class MyErrorListener implements Response.ErrorListener {
        @Override
        public void onErrorResponse(VolleyError error) {
            image.setImageResource(R.drawable.error_icon);
        }
    }

    private class SubmitButtonOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            url = insertedLink.getText().toString();
            showImage(url);
        }
    }
}

My xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/insertLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/url"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/insertLink"
        android:layout_centerHorizontal="true"
        android:text="@string/btn"/>
    <ImageView
        android:id="@+id/ivImageView"
        android:layout_below="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="50dp"/>


</RelativeLayout>
Natalie
  • 163
  • 1
  • 3
  • 15

0 Answers0