2012-01-28 171 views
20

我有一個畫廊,顯示圖像數組,當點擊它們時,它們顯示在圖像視圖中。我希望能夠分享當前在意向選擇器中顯示的圖像。我無法弄清楚如何選擇當前圖像。從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我需要爲當前圖像的變量莫名其妙。有任何想法嗎?

回答

55

爲了獲取當前選定的視圖使用

Gallery.getSelectedView(); 

,並從ImageView的使用越來越繪製對象:

ImageVIew.getDrawable() 

如果你想下面從繪製使用獲得的InputStream:

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

那麼如何將當前的imageview圖像轉換爲字符串。例如上面的代碼中的R.drawable.alright。使用imageview.getdrawable.tostring()我得到[email protected] – 2012-01-28 05:08:29

+1

儘管我沒有使用,只是嘗試通過使用:資源.getResourcePackageName(resId)+'/'+ resources.getResourceTypeName(resId)+' /'+ resources.getResourceEntryName(resId); – jeet 2012-01-28 06:09:07

+0

我可以在這個程序中找到可繪製的變量 – 2018-02-06 08:50:04

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

} 
相關問題