Sidewalk: The IoT network from Amazon
08.05.2024
Elektronik | Funk | Software
Der Technik-Blog
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 |
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.
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" />
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 moreOn 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 moreAEQ-WEB © 2015-2024 All Right Reserved