从ImageView获取图像

我有一个显示图像数组的图库,点击它们时会显示在图像视图中。 我希望能够分享当前在意图选择器中显示的图像。 我无法弄清楚如何选择当前图像。

图库代码:

public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mImageIds[position]); imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setBackgroundResource(mGalleryItemBackground); return imageView; } 

意图选择代码:

 Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/png"); share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://com.appinfluence.fanapp.v1/drawable/" + Integer.toString(R.drawable.alright))); startActivity(Intent.createChooser(share, "Share Image")); 

在哪里说R.drawable.alright我需要它以某种方式成为当前图像的variables。 有任何想法吗?

Solutions Collecting From Web of "从ImageView获取图像"

要获取当前选定的视图使用

 Gallery.getSelectedView(); 

并从imageView获取Drawable使用:

 ImageVIew.getDrawable() 

如果您想从drawable使用以下输入流:

 BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable); Bitmap bitmap = bitmapDrawable .getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] imageInByte = stream.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte); 
  l.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> parent, View view, int position, long id) { TextView textView=(TextView)view.findViewById(R.id.textView); ImageView imageView=(ImageView)view.findViewById(R.id.imageView); String textViewString=textView.getText().toString(); Bitmap image=((BitmapDrawable)imageView.getDrawable()).getBitmap(); DialogClass dialogClass=new DialogClass(MainActivity.this,image,textViewString); dialogClass.show(); } }); 

我最好的function

 public class MainActivity extends Activity { private ImageView imgView,bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgView=(ImageView) findViewById(R.id.imgView); bitmap=(ImageView) findViewById(R.id.bitmap); //set view to bitmap image bitmap.setImageBitmap(convertImageViewToBitmap(imgView)); } //function to convert imageView to Bitmap private Bitmap convertImageViewToBitmap(ImageView v){ Bitmap bm=((BitmapDrawable)v.getDrawable()).getBitmap(); return bm; } }