【问题标题】:Getting Image from ImageView从 ImageView 获取图像
【发布时间】:2012-02-21 00:16:01
【问题描述】:

我有一个显示图像数组的画廊,当单击它们时,它们会显示在图像视图中。我希望能够共享当前显示在意图选择器中的图像。我不知道如何选择当前图像。

图库代码:

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 我需要它以某种方式成为当前图像的变量。有什么想法吗?

【问题讨论】:

    标签: android android-imageview android-gallery


    【解决方案1】:

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

    Gallery.getSelectedView(); 
    

    以及从 imageView 获取 Drawable 使用:

    ImageVIew.getDrawable()
    

    如果您想从可绘制对象中获取输入流,请使用以下内容:

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

    【讨论】:

    • 那么我如何将当前的imageview图像转换成一个字符串。例如我上面的代码中的 R.drawable.alright。使用 imageview.getdrawable.tostring() 我得到 android.drawable.BitmapDrawable@414743f8
    • 虽然我没用过,但是试试用:resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId);
    • 我在哪里可以找到这个程序的可绘制变量
    【解决方案2】:
        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();
            }
        });
    

    【讨论】:

      【解决方案3】:

      我的最佳功能

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

      【讨论】:

        猜你喜欢
        • 2013-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-17
        相关资源
        最近更新 更多