Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: image

Drawable localization



Open a folder with name ‘drawable-en’ when the local setting is ‘en’.
My default language is ‘zh’, so the image with chines word store at ‘drawable’ folder.
At the same time, we should have ‘values’ and ‘values-en’ folders.

Read more of this post

Android ‘Out of memory’ when loading image

To solve ‘Out of memory’ when loading image, you must control the image size and image quality.
This article are go to solve the problem of out of memory when loading a huge image into a image view.

Besides, it provide lazy load automatically.

Universal Image Loader
Home page of Universal Image Loader

Install by config build.gradle(Module):

dependencies {
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'


In MyApplication class(Your custom Application class), onCreate(), write down:

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        .displayer(new FadeInBitmapDisplayer(300)).build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
        .memoryCache(new WeakMemoryCache())
        .discCacheSize(100 * 1024 * 1024).build();


In your activity, use the following code to load image:

File imgFile = new File(ImageUtility.getSnapShotDirectoryPath() + path);
ImageView imgSnapShotFull = (ImageView) findViewById(;
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("file://" + imgFile.getAbsolutePath(), imgSnapShotFull);

You can control the cache size or sample size to do a advanced loading.
Please follow the instruction at the Home page.

Android WebView Upload Picture via Camera

When clicking an Input File button on webview,
we want to take a picture from camera and place the image to that input.

1. Declare instance variables

private final static int CAPTURE_RESULTCODE = 1;
private ValueCallback<Uri> mUploadMessage;
private String filePath;

2. Set custom webChromeClient to WebView

myWebView.setWebChromeClient(new WebChromeClient() {				
	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {

	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {

	public void openFileChooser(ValueCallback<Uri> uploadMsg) {
		this.mUploadMessage = uploadMsg;

		File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "appName");
		this.filePath = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
		File file = new File(this.filePath);

		Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
		MainActivity.this.startActivityForResult(captureIntent,  CAPTURE_RESULTCODE); 

3. Apply onActivityResult to handle the picture after capture

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	if (requestCode == CAPTURE_RESULTCODE) {
		if (null == this.mUploadMessage || (resultCode != RESULT_OK && !new File(filePath).exists())) {
		} else {
			ContentValues values = new ContentValues();
			values.put(MediaStore.Images.Media.DATA, this.filePath);
			this.mUploadMessage.onReceiveValue(this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values));
		this.mUploadMessage = null;

Make Background Image Fitting to the UIView

1.             Too Small                                    Too Large                                What we want
Too SmallToo LargeFit

2. To make a centered and stretched image:

[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:image];