我有一个显示图像数组的图库,点击它们会在图像视图中显示。我希望能够分享当前在意图选择器中显示的图像。我无法弄清楚如何选择当前图像。
图库代码:
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我需要它以某种方式成为当前图像的变量。有什么想法吗?
答案 0 :(得分:63)
要获取当前选定的视图,请使用
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);
答案 1 :(得分:3)
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();
}
});
答案 2 :(得分:0)
我的最佳功能
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;
}
}