Demo entry 3662918

Hello

   

Submitted by anonymous on Feb 17, 2016 at 14:56
Language: Java. Code size: 4.8 kB.

package com.andideveloper.activity.com;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {

	TextView textView;
	ImageView imageView;
	Button button;
	Bitmap bitmap;

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

		textView = (TextView) findViewById(R.id.textView);
		imageView = (ImageView) findViewById(R.id.image);
		button = (Button) findViewById(R.id.save);

		// for check internet connection
		
		Boolean internetConnection = CheckNetConnection // if connection is
														// there, it will return
														// true otherwise false
				.isNetworkAvailable(getApplicationContext());
		if (internetConnection == false) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Internet Connection Issue");
			builder.setMessage("No Internet Connection !")
					.setCancelable(false)
					.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {

								}
							});
			AlertDialog alert = builder.create();
			alert.show();
		}
		else {
			new FetchImage().execute("http://url-of-your-server/img_url.png");
		}
	}

	public class FetchImage extends AsyncTask<String, String, Bitmap> {
		@Override
		protected void onPreExecute() {
			try {
				textView.setText("Loading Image..");
				super.onPreExecute();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		protected Bitmap doInBackground(String... args) {
			try {
				bitmap = BitmapFactory.decodeStream((InputStream) new URL( // fetch
																			// image
																			// from
																			// url
																			// and
																			// convert
																			// in
																			// bitmap
						args[0]).getContent());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return bitmap;
		}

		protected void onPostExecute(final Bitmap image) {
			try {
				if (image != null) {
					imageView.setImageResource(0);
					imageView.setImageBitmap(image);
					textView.setText("");
					button.setOnClickListener(new View.OnClickListener() { // for
																			// makeing
																			// bitmap
																			// to
																			// iage
																			// file
																			// and
																			// save
																			// in
																			// gallary
						@Override
						public void onClick(View v) {
							String name = image.toString();
							File sdCardDirectory = Environment
									.getExternalStorageDirectory();
							File image_file = new File(sdCardDirectory, name
									+ ".png");
							FileOutputStream outStream;
							try {
								outStream = new FileOutputStream(image_file);
								image.compress(Bitmap.CompressFormat.PNG, 100,
										outStream);
								outStream.flush();
								outStream.close();
								Intent mediaScanIntent = new Intent(
										Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
								Uri contentUri = Uri.fromFile(image_file);
								mediaScanIntent.setData(contentUri);
								getApplicationContext().sendBroadcast(
										mediaScanIntent);
								Toast.makeText(getApplicationContext(),
										"Image Download Successfully",
										Toast.LENGTH_SHORT).show();
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}

						}
					});

				} else {
					Toast.makeText(getApplicationContext(),
							"Image Does Not exist or Network Error",
							Toast.LENGTH_SHORT).show();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).