2015-09-06 80 views
0

我有一個展示圖像集合的圖庫,我只是發現要共享顯示的圖像。 我知道圖像應該保存在CD卡中,然後共享它。 但我的主要問題是誰告訴哪個ID必須共享。在android項目中共享圖像

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(); 
      } 
     } 
    }; 

我不知道該怎麼IDS設置此功能。

回答

0

也許我不太瞭解你的問題,但... 你可以保存圖像到存儲卡。好。 爲什麼要分享存儲卡中的圖像? 您可以使用功能共享與您的畫廊。

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.."));