多个附件,但只有选定的附件

时间:2012-03-08 11:53:54

标签: android email-attachments

我的主视图包含与其关联的图像和复选框以及发送电子邮件按钮。当我单击复选框并选择发送电子邮件按钮时,我应该被重定向到必须附加所选图像的电子邮件页面。这是我的代码。 请帮帮我

public class GridcheckboxActivity extends Activity {
    GridView mygrid;
    String[] imagepaths;
    Uri[] myUris;
    boolean[] ticking;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mygrid=(GridView)findViewById(R.id.gridView1);
        String folderpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/New Folder/";
        File myfile=new File(folderpath);
        File[] imageslist=myfile.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                // TODO Auto-generated method stub
                return (filename.toLowerCase().endsWith("jpg")||filename.toLowerCase().endsWith("png")||filename.toLowerCase().endsWith("jpeg"));
            }
        });
        int imgCount=imageslist.length;
        imagepaths=new String[imgCount];
        for(int i=0;i<imgCount;i++){
            imagepaths[i]=imageslist[i].getAbsolutePath();
            System.out.println("my image's paths are::::::::"+imagepaths[i]);
        }
        this.ticking=new boolean[imgCount];
        ImageAdapter imgad=new ImageAdapter();
        mygrid.setAdapter(imgad);
        Button select=(Button)findViewById(R.id.button1);
        select.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int len = ticking.length;
                int cnt = 0;
                String selectImages = "";
                ArrayList<String> myarray=new ArrayList<String>();

                for (int i =0; i<len; i++)
                {
                    if (ticking[i]){
                    cnt++;
                    selectImages = selectImages + imagepaths[i] + "|";
                    myarray.add(imagepaths[i]);
                    }
                }
                Log.d("myarray","myarray size==="+myarray.size());
                if (cnt == 0){
                     Toast.makeText(getApplicationContext(),"Please select at least one image",Toast.LENGTH_LONG).show();
                          }
                else {
                      Toast.makeText(getApplicationContext(), "You've selected Total " + cnt + " image(s).",Toast.LENGTH_LONG).show();
                      Log.d("SelectedImages", selectImages);

                 try{
                        Intent emailintent=new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
                        emailintent.setType("application/octet-stream");

                        String[] addressvalue=new String[]{"user@domain.example"};
                        emailintent.putExtra(android.content.Intent.EXTRA_EMAIL, addressvalue);
                        emailintent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subjectvalue");
                        String bccvalue[]={"bcc address"};
                        emailintent.putExtra(android.content.Intent.EXTRA_BCC, bccvalue);
                        String ccvalue[]={"cc address"};
                        emailintent.putExtra(android.content.Intent.EXTRA_CC,ccvalue );
                        ArrayList<Uri> newone=new ArrayList<Uri>();
                        for(int j=0;j<myarray.size();j++){
                        Uri u=Uri.parse("file:/"+myarray.get(j));
                        Log.d("uris", "myuris are:::::::"+u);
                        newone.add(u);
                        }
                        System.out.println("my uri array has values------->"+newone);
                        emailintent.putExtra(android.content.Intent.EXTRA_STREAM, newone);

                        GridcheckboxActivity.this.startActivity( Intent.createChooser(emailintent, "sending email using:"));
                    }catch(Exception e){
                        e.printStackTrace();
                        Log.d("error", "cannot start activity");
                    }
                }
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

检查你的xml&amp;这个属性到列表android:choiceMode =“multipleChoice”并看到。

答案 1 :(得分:0)

我尝试了很多,并找到了我的问题的答案

public class GridcheckboxActivity extends Activity {
    GridView mygrid;
    String[] imagepaths;
    Uri[] myUris;
    boolean[] ticking;
    ArrayList<String> myarray=new ArrayList<String>();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mygrid=(GridView)findViewById(R.id.gridView1);
        String folderpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/New Folder/";
        File myfile=new File(folderpath);
        File[] imageslist=myfile.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                // TODO Auto-generated method stub
                return (filename.toLowerCase().endsWith("jpg")||filename.toLowerCase().endsWith("png")||filename.toLowerCase().endsWith("jpeg"));
            }
        });
        int imgCount=imageslist.length;
        imagepaths=new String[imgCount];
        for(int i=0;i<imgCount;i++){
            imagepaths[i]=imageslist[i].getAbsolutePath();
            System.out.println("my image's paths are::::::::"+imagepaths[i]);
        }
        this.ticking=new boolean[imgCount];
        ImageAdapter imgad=new ImageAdapter();
        mygrid.setAdapter(imgad);
        Button select=(Button)findViewById(R.id.button1);
        select.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int len = ticking.length;
                int cnt = 0;
                String selectImages = "";


                for (int i =0; i<len; i++)
                {
                    if (ticking[i]){
                    cnt++;
                    selectImages = selectImages + imagepaths[i] + "|";
                    myarray.add(imagepaths[i]);
                    }
                }
                Log.d("myarray","myarray size==="+myarray.size());
                if (cnt == 0){
                     Toast.makeText(getApplicationContext(),"Please select at least one image",Toast.LENGTH_LONG).show();
                          }
                else {
                      Toast.makeText(getApplicationContext(), "You've selected Total " + cnt + " image(s).",Toast.LENGTH_LONG).show();
                      Log.d("SelectedImages", selectImages);

                 try{
                        Intent emailintent=new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
                        emailintent.setType("application/octet-stream");
                        String[] addressvalue=new String[]{"user@domain.example"};
                        emailintent.putExtra(android.content.Intent.EXTRA_EMAIL, addressvalue);
                        emailintent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subjectvalue");
                        String bccvalue[]={"bcc address"};
                        emailintent.putExtra(android.content.Intent.EXTRA_BCC, bccvalue);
                        String ccvalue[]={"cc address"};
                        emailintent.putExtra(android.content.Intent.EXTRA_CC,ccvalue );
                        ArrayList<Uri> axn=getUriListForImages();
                        emailintent.putExtra(android.content.Intent.EXTRA_STREAM, axn);

                        GridcheckboxActivity.this.startActivity( Intent.createChooser(emailintent, "sending email using:"));
                    }
                 catch(Exception e){
                        e.printStackTrace();
                        Log.d("error", "cannot start activity");
                    }
                }
            }



        });
    }
private ArrayList<Uri> getUriListForImages() throws Exception {
    ArrayList<Uri> myList = new ArrayList<Uri>();
    String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/New Folder/";
    if(myarray.size() != 0) {
        for(int i=0; i<myarray.size(); i++)
        {   
            try 
            {   
                ContentValues values = new ContentValues(7);
                values.put(Images.Media.TITLE, myarray.get(i));
                values.put(Images.Media.DISPLAY_NAME, myarray.get(i));
                values.put(Images.Media.DATE_TAKEN, new Date().getTime());
                values.put(Images.Media.MIME_TYPE, "image/jpeg");
                values.put(Images.ImageColumns.BUCKET_ID, imageDirectoryPath.hashCode());
                values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, myarray.get(i));
                values.put("_data", myarray.get(i));
                ContentResolver contentResolver = getApplicationContext().getContentResolver();
                Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
                myList.add(uri);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return myList;
} 

    public class ImageAdapter extends BaseAdapter{
        private LayoutInflater mInflater;
        //Context mycontext;
        public ImageAdapter() {
            // TODO Auto-generated constructor stub

            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imagepaths.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return imagepaths[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View conview, ViewGroup arg2) {
            // TODO Auto-generated method stub
            viewholder myholder;
            if(conview== null){
                myholder=new viewholder();
                conview=mInflater.inflate(R.layout.inflatexml,null);
                myholder.img=(ImageView) conview.findViewById(R.id.imageView1);
                myholder.cbox=(CheckBox) conview.findViewById(R.id.checkBox1);
                conview.setTag(myholder);

            }else{
                myholder=(viewholder) conview.getTag();
            }
            myholder.img.setId(position);
            myholder.cbox.setId(position);

            myholder.cbox.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CheckBox cb=(CheckBox)v;
                    int id=cb.getId();
                    if(ticking[id]){
                        cb.setChecked(false);
                        ticking[id]=false;
                    }else{
                        cb.setChecked(true);
                        ticking[id]=true;
                    }
                }
            });
            myholder.img.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id=v.getId();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse("file:/"+imagepaths[id]), "image/*");               
                    startActivity(intent);


                }
            });

            Bitmap bm=BitmapFactory.decodeFile(""+imagepaths[position]);
            Bitmap bm1=Bitmap.createScaledBitmap(bm, 140, 200, true);
            myholder.img.setImageBitmap(bm1);
            myholder.cbox.setChecked(ticking[position]);
            myholder.cbox.setGravity(Gravity.TOP);
            myholder.id=position;
            return conview;
        }


    }
    class viewholder{
        ImageView img;
        CheckBox cbox;
        int id;
    }

}