Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Download file from URL

Download image file from URL to ImageView Java Android source example code.



Context context = thisClass.this;
Drawable image = ImageOperations(context,
"//www.okhelp.cz/images/android/ad_4.png"
,"image.jpg");
ImageView imgView;
imgView = (ImageView)findViewById(R.id.idImageView);
imgView.setImageDrawable(image);

	private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}

396LW NO topic_id




AD

Další témata ....(Topics)


82

Switch Statement Java basic example | switch-statement-java-basic-example


Switch statement with numbers and array of strings Java example.


public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = { "One", "Two", "Three", "Four" };

		int i = 2;
		switch (i) {
			case 1: {
				System.out.println(arrayOfString[i]);
				break;
			}
			case 2: {
				System.out.println(arrayOfString[i]);
				break;
			}
			case 3: {
				System.out.println(arrayOfString[i]);
				break;
			}
			default: {
				System.out.println("Enter a valid value.");
			}
		} // END of switch
	}
}
/*
 * Three
 */




Possible:

case 1:
 System.out.println(arrayOfString[i]);
break;

// i love this notation 
case 1:{
 System.out.println(arrayOfString[i]);
}break;

case 1:{
 System.out.println(arrayOfString[i]);
 break;
}

321

View overrides onTouchEvent but not performClick | view-overrides-ontouchevent-but-not-performclick



public class Panel extends SurfaceView implements SurfaceHolder.Callback {
//............... code
//............... some code
   /**
     * Process the MotionEvent.
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	
        synchronized (getHolder()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
            	performClick();
             } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            	if(_currentGraphic==null)return true;
                
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
            }
            return true;
        }
    }
/////////////////////////////
 @Override
 public boolean performClick() {
  // Calls the super implementation, which generates an AccessibilityEvent
        // and calls the onClick() listener on the view, if any
        super.performClick();

        // Handle the action for the custom click here

        return true;
 }

}

233

tag should specify a target API level Warning | uses-sdk-tag-should-specify-target-api


Warning in AndroidManifest.xml:
tag should specify a target API level (the highest verified version; when running on
later versions, compatibility behaviors may be enabled) with android:targetSdkVersion="?"

Solution:

    <uses-sdk android:minSdkVersion="4"
         android:targetSdkVersion="16" />
183

Transparent Background Android example | transparent-background-android-example


android:background="@android:color/transparent"

<LinearLayout
    android:baselineAligned="false"
    android:background="@android:color/transparent"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
   >
<LinearLayout>
////////////////////////////
LinearLayout  mLinearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1);
mLinearLayout1.setBackgroundColor(Color.TRANSPARENT);

222

Disable enable internet connection in Android Emulator | disable-enable-internet-connection-in-android-emulator


If you try function for checking internet connection you can disable internet on the emulator:
Settings - Wireless and networks - Mobile networks - Data enabled (checked - unchecked )


 public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) 
          getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }