在android项目中共享图像

时间:2015-09-06 18:38:17

标签: java android

我有画廊,显示图像集合,我只是找到分享显示的图像。 我知道图像应保存在CD卡中,然后分享。 但我的主要问题是谁告诉谁必须分享哪个ID。

enter code here:
  public class ImageAdapter extends BaseAdapter {
    private Context context;
    private int itemBackground;
    public ImageAdapter(Context c)
    {
        context = c;
        // sets a grey background; wraps around the images
        TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
        itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
        a.recycle();
    }
    // returns the number of images

    // returns an ImageView view
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
    public int getCount() {
        return imageIDs.length;
    }
    // returns the ID of an item
    public Object getItem(int position) {
        return position;
    }
    // returns the ID of an item
    public long getItemId(int position) {
        return position;
    }

}







//the images to display
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7,



};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView = (ImageView) findViewById(R.id.share);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
        // Note that Gallery view is deprecated in Android 4.1---
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, final int position,long id)
    {
            Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected",
                    Toast.LENGTH_SHORT).show();


                // display the images selected
                ImageView imageView = (ImageView) findViewById(R.id.image1);
                imageView.setImageResource(imageIDs[position]);




    }
    });
}

这是我的代码,现在我想以这种方式分享我的图片:

enter code here


 public void share(View view)
    {

        Handler handler = new Handler();
        handler.post(runnable);
    }
    final Runnable runnable = new Runnable()
    {




        public void run()
        {




            Bitmap bitmap;
            OutputStream output;




            // Retrieve the image from the res folder
            int s = getCount();
                    bitmap = BitmapFactory.decodeResource(getResources(),s);

            // Find the SD Card path
            File filepath = Environment.getExternalStorageDirectory();

            // Create a new folder AndroidBegin in SD Card
            File dir = new File(filepath.getAbsolutePath() + "/Share Image /");
            dir.mkdirs();

            // Create a name for the saved image
            File file = new File(dir, "" + ".png");

            try {

                // Share Intent
                Intent share = new Intent(Intent.ACTION_SEND);

                // Type of file to share
                share.setType("image/png");

                output = new FileOutputStream(file);

                // Compress into png format image from 0% - 100%
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
                output.flush();
                output.close();

                // Locate the image to Share
                Uri uri = Uri.fromFile(file);

                // Pass the image into an Intnet
                share.putExtra(Intent.EXTRA_STREAM, uri);

                // Show the social share chooser list
                startActivity(Intent.createChooser(share, "Share Image Tutorial"));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

我不知道如何将id设置为此函数。

2 个答案:

答案 0 :(得分:0)

也许我不太明白你的问题,但...... 您可以将图像保存到存储卡。好。 为什么要从存储卡分享图像? 您可以在图库中使用功能共享。

答案 1 :(得分:0)

分享单张图片

Intent shareIntent = new Intent();   
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent,                                          
getResources().getText(R.string.send_to)));

分享多张图片

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));