0

如何

  1. 从图库中选择图像。

  2. 获取此图像的 Uri。

  3. 将 Uri 传递给另一个 Activity。[shared preffrence's ??]

  4. 使用 uri 加载图像。

将其设置为 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);

        }
}
4

2 回答 2

1

启动“获取图像”意图:

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) {
    // ...
}

获取 Uri 并传递给新的 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);
    }
}

在新的 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;
}
于 2013-05-05T03:34:13.697 回答
0

最终工作代码:

ChangeBackground.java 在此处从图库中选择图像。

Inbox.java 显示收件箱中的消息,选择图像作为背景。

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;
        }
}
于 2013-05-05T04:44:27.957 回答