package com.bumptech.glide.supportapp.github._906_cache_put;


import android.annotation.*;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build.VERSION_CODES;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.cache.DiskCache.Writer;
import com.bumptech.glide.load.resource.bitmap.*;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.signature.StringSignature;
import com.bumptech.glide.supportapp.*;

public class TestFragment extends GlideImageFragment {
	@Override protected void load(Context context) throws Exception {
				.load(Uri.parse("file:///sdcard/001.jpg")) // doesn't matter what model you load
				.override(2000, 2000) // size of the image to upload
				.listener(new RequestListener<Uri, Bitmap>() {
					@Override public boolean onException(Exception e, Uri model, Target<Bitmap> target,
							boolean isFirstResource) {
						return false;
					@Override public boolean onResourceReady(Bitmap resource, Uri model, Target<Bitmap> target,
							boolean isFromMemoryCache, boolean isFirstResource) {
						return false;
	private void upload(Bitmap resource) {
		new AsyncTask<Bitmap, Void, String>() {
			@Override protected String doInBackground(Bitmap... params) {
				Bitmap bitmap = params[0];
				String url = "";
				// TODO upload bitmap and get a resulting url,
				// placeholder is there to show that it works even with a malformed non-existent url
				cache(url, bitmap);
				return url;
			@Override protected void onPostExecute(String url) {
						.diskCacheStrategy(DiskCacheStrategy.SOURCE) // must use this to hit the just-cached image

	private void cache(String url, final Bitmap bitmap) {
		Key key = new StringSignature(url);
		// the key here is that Engine uses fetcher.getId() for constructing OriginalKey from EngineKey
		// see Engine.load and also signature can be ignored because it is an EmptySignature instance for most
		App.getInstance().getDiskCache().put(key, new Writer() {
			@TargetApi(VERSION_CODES.KITKAT) // for try-with-resources
			@Override public boolean write(File file) {
				try (OutputStream out = new FileOutputStream(file)) {
					// mimic default behavior you can also use Bitmap.compress
					BitmapPool pool = Glide.get(getContext()).getBitmapPool();
					BitmapResource resource = BitmapResource.obtain(bitmap, pool);
					new BitmapEncoder().encode(resource, out);
					return true;
				} catch (IOException e) {
					return false;