Images for Android Photo Filter Tutorials

+2 Bucky Roberts · January 10, 2015


package com.thenewboston.photofilter;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Environment;
import android.provider.MediaStore;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    ImageView buckysImageView;
    Drawable buckysFace;
    Bitmap bitmapImage;

    protected void onCreate(Bundle savedInstanceState) {

        buckysImageView = (ImageView) findViewById(;

        buckysFace = getResources().getDrawable(R.drawable.bucky);
        bitmapImage = ((BitmapDrawable) buckysFace).getBitmap();
        Bitmap newPhoto = invertImage(bitmapImage);

        Drawable[] layers = new Drawable[2];
        layers[0] = getResources().getDrawable(R.drawable.bucky);
        layers[1] = getResources().getDrawable(R.drawable.dirty);
        LayerDrawable layerDrawable = new LayerDrawable(layers);

        //Save the image to the users device
        MediaStore.Images.Media.insertImage(getContentResolver(), newPhoto, "title", "description");

    //Invert a bitmap image
    public static Bitmap invertImage(Bitmap original){
        Bitmap finalImage = Bitmap.createBitmap(original.getWidth(), original.getHeight(), original.getConfig());

        int A, R, G, B;
        int pixelColor;
        int height = original.getHeight();
        int width = original.getWidth();

        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                pixelColor = original.getPixel(x, y);
                A = Color.alpha(pixelColor);
                R = 255 -;
                G = 255 -;
                B = 255 -;
                finalImage.setPixel(x, y, Color.argb(A, R, G, B));
        return finalImage;


Post a Reply


Oldest  Newest  Rating
0 Roman Smirnov · February 23, 2015
I tried using the insertImage() method to add my photo to gallery 
MediaStore.Images.Media.insertImage(getContentResolver(), newPhoto, "title", "description"); 
I added the permission    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The image doesn't get added to the gallery nor does it appear in any place I can figure out on the device
It returns content://media/external/images/media/19990 as String.

Any ideas?
0 Rip Van Winkle · August 18, 2015
Fun example!

I did have to make one change, since apparently the old version of getDrawable got deprecated as of API 22. (They replaced it with a new version of getDrawable with an additional parameter for "theme", and I suppose it must make everything look just beautiful, but the new version seems to only work on API 22 and up, which almost nobody has installed yet.) Anyway, there's a compatible version called ContextCompat.getDrawable which seems to solve the problem in general.

So if anyone sees getDrawable crossed out in their code, replace these two lines
        layers[0] = getResources().getDrawable(R.drawable.bucky);
       layers[1] = getResources().getDrawable(R.drawable.dirty);

with these two:
layers[0] = ContextCompat.getDrawable(this, R.drawable.bucky);
layers[1] = ContextCompat.getDrawable(this, R.drawable.dirty);

... and that should fix it.

Except I had one other problem with the last part of the exercise. Like Roman, I tried to find where the image had been saved, and couldn't find it anywhere. I thought that maybe it was because I was testing using the emulator, and not an actual device. But if the image was stored somewhere, I sure couldn't find it.
0 tokinonagare W. · September 7, 2015
layers[0] = ResourcesCompat.getDrawable(getResources(), R.drawable.avatar, null);
layers[1] = ResourcesCompat.getDrawable(getResources(), R.drawable.dirty, null);

Would solve the  deprecated problem!
0 nathan abraham · May 17, 2016
How do get all these source codes?
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator