Events

:

:

Elektronik | Funk | Software

Der Technik-Blog

  • Social Media

    Werbung:


    New Posts


    Events

    • Keine zukünftigen Events vorhanden

    The Tech-Blog

    Android Image Download from PHP Java Code

    Android Image Download

    Alex @ AEQ-WEB

    On this page, we will show you how to download an image from a PHP file with Android using the HTTP client. At the bottom of the app there is a button, which starts the task for the download process. After a successful download in the task the photo is loaded into the ImageView. After the download, the result of the download is displayed in the TextView.

    Android Version API Version
    4.0.3 15

    PHP Server

    The PHP script provides the data for the app. The photo is loaded directly from the php file and not from a path with the image file. This has the advantage that not every image has its own file. With the GET-parameters, the picture output can be controlled. In addition, the script can be modified so that the image is displayed only after authorization. The PHP code used here has three images of which it randomly outputs one. This example is also available on our testserver and is compatible with the app.


    Werbung:

    Image Server
    Download
    HTTP_Image.apk
    Download
    HTTP_Image Source
    Download

    MainActivity: This section must be copied to MainActivity.

    
    public class MainActivity extends AppCompatActivity {
    
        TextView txt;
        boolean download_ok;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            txt = (TextView)findViewById(R.id.textView);
            Button button= (Button) findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new DownloadImageFromInternet((ImageView) findViewById(R.id.imageView))
                            .execute("http://testserver.aeq-web.com/android_load_image/index.php");
                }
            });
        }
        private class DownloadImageFromInternet extends AsyncTask {
            ImageView imageView;
    
            public DownloadImageFromInternet(ImageView imageView) {
                this.imageView = imageView;
                txt.setTextColor(Color.GRAY);
                txt.setText("Try to download image....");
            }
    
            protected Bitmap doInBackground(String... urls) {
                String imageURL = urls[0];
                Bitmap d_image = null;
                try {
                    InputStream in = new java.net.URL(imageURL).openStream();
                    d_image = BitmapFactory.decodeStream(in);
                    download_ok = true;
                } catch (Exception e) {
                    download_ok = false;
                    e.printStackTrace();
                }
                return d_image;
            }
    
            protected void onPostExecute(Bitmap result) {
                txt_update();
                imageView.setImageBitmap(result);
            }
    
            void txt_update (){
                if(download_ok == true) {
                    txt.setTextColor(Color.GREEN);
                    txt.setText("Image was successfully downloaded");
                }else {
                    txt.setTextColor(Color.RED);
                    txt.setText("Image could not be downloaded");
                }
            }
        }
    
        }
    

    Manifest: This line must copied to the manifest and will allow access to the Internet.

    <uses-permission android:name="android.permission.INTERNET" />


    Info: This page was automatically translated and may contain errors
    122X122

    About the Author

    Alex, the founder of AEQ-WEB. He works for more of 10 years with different computers, microcontroller and semiconductors. In addition to hardware projects, he also develops websites, apps and software for computers.

    Top articles in this category:

    Android HTTP Request - Async Task

    Android HTTP Request

    • DE/EN

    On this page we show you how to make an HTTP request with android. This code also supports HTTPS and can transmit POST and GET parameters

    read more
    Android Notifications Java Code - Android Studio

    Android Notifications

    • DE/EN

    On this page we show you how to create notifications with android. The notification is displayed in the status bar and can be opened by clicking

    read more

    Social Media

    Werbung:


    New Posts


    Events

    • Keine zukünftigen Events vorhanden