hl_joker
hl_joker

Reputation: 61

How to select an image from gallery use it in another activty

How to

  1. Select an image from gallery.

  2. Get Uri of this image.

  3. Pass Uri to another Activity.[shared preffrence's ??]

  4. Load image using uri.

Set it as backgound of Inbox Activity:

@SuppressLint("NewApi")
public class Inbox extends ListActivity 
{          
        ArrayList<String> ListItems = new ArrayList<String>();
        ArrayAdapter<String> adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Uri urisms = Uri.parse("content://sms/inbox");
               Cursor c = getContentResolver().query(urisms, null, null ,null,null);
               if(c.moveToFirst())
               {
                             for(int i=0; i < c.getCount(); i++)
                             {   String body = c.getString(c.getColumnIndexOrThrow("body")).toString();
                                   ListItems.add(body);
                                 c.moveToNext();
                             }
                             if(ListItems.isEmpty())
                                 ListItems.add("no messages found !!");
                }
                c.close();
                adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ListItems);
                setListAdapter(adapter);

        }
}

Upvotes: 0

Views: 117

Answers (2)

Paul Burke
Paul Burke

Reputation: 25584

Launch a "get image" intent:

public static final int REQUEST_CODE = 0;

Intent target = new Intent(Intent.ACTION_GET_CONTENT); 
target.setType("image/*"); 
target.addCategory(Intent.CATEGORY_OPENABLE);
Intent intent = Intent.createChooser(target, "Choose Image");
try {
    startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
    // ...
}

Get Uri and pass to new Activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        // The Uri of the Image selected
        Uri uri = data.getData();

        Intent i = new Intent(this, NewActivity.class);
        i.setData(uri);
        startActivity(i);
    }
}

Set as the background in your new ListActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Uri uri = getIntent().getData();
    Drawable drawable = getDrawable(uri);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        getListView().setBackgroundDrawable(drawable);
    } else {
        getListView().setBackground(drawable);
    }
}

private Drawable getDrawable(Uri uri) {
    Drawable drawable = null;
    InputStream is = null;
    try {
        is = getContentResolver().openInputStream(uri);
        drawable = Drawable.createFromStream(is, null);
    } catch (Exception e) {
        // ...
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e2) {
            }
        }
    }
    return drawable;
}

Upvotes: 1

hl_joker
hl_joker

Reputation: 61

Final Working code :

ChangeBackground.java Choose Image from gallery here .

Inbox.java Show's Messages from inbox with choosen Image as Background.

public class ChangeBackground extends Activity{

    private static final int REQUEST_CODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Intent target = new Intent(Intent.ACTION_GET_CONTENT); 
        target.setType("image/*"); 
        target.addCategory(Intent.CATEGORY_OPENABLE);
        Intent intent = Intent.createChooser(target, "Choose Image");
        try {
            startActivityForResult(intent, REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            // ...
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            Uri uri = data.getData();

            Intent i = new Intent(this,Inbox.class);
            i.setData(uri);
            startActivity(i);
        }
    }
}//end of ChangeBackground
public class Inbox extends ListActivity 
{          
        ArrayList<String> ListItems = new ArrayList<String>();
        ArrayAdapter<String> adapter;

        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Uri uri = getIntent().getData();
            Drawable drawable = getDrawable(this, uri);
            getListView().setBackgroundDrawable(drawable);

            Uri urisms = Uri.parse("content://sms/inbox");
               Cursor c = getContentResolver().query(urisms, null, null ,null,null);
               if(c.moveToFirst())
               {
                             for(int i=0; i < c.getCount(); i++)
                             {   String body = c.getString(c.getColumnIndexOrThrow("body")).toString();
                                   ListItems.add(body);
                                 c.moveToNext();
                             }
                             if(ListItems.isEmpty())
                                 ListItems.add("no messages found !!");
                }
                c.close();
                adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ListItems);
                setListAdapter(adapter);

        }

            private Drawable getDrawable(Context context, Uri uri) {
            Drawable drawable = null;
            InputStream is = null;
            try {
                is = context.getContentResolver().openInputStream(uri);
                drawable = Drawable.createFromStream(is, null);
            } catch (Exception e) {
                // ...
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e2) {
                    }
                }
            }
            return drawable;
        }
}

Upvotes: 0

Related Questions