2012-01-28 40 views
20

Tôi có một thư viện hiển thị một loạt hình ảnh, khi được nhấp vào chúng được hiển thị trong một lần xem hình ảnh. Tôi muốn có thể CHIA SẺ hình ảnh hiện đang được hiển thị trong trình chọn ý định. Tôi không thể tìm ra cách chọn hình ảnh hiện tại.Lấy hình ảnh từ ImageView

đang Thư viện ảnh:

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

Ý định đang chooser:

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

đâu nó nói R.drawable.alright tôi cần đó là một biến của hình ảnh hiện tại bằng cách nào đó. Bất kỳ ý tưởng?

Trả lời

55

Để lấy được chọn hiện thời sử dụng xem

Gallery.getSelectedView(); 

và để nhận drawable việc sử dụng IMAGExem:

ImageVIew.getDrawable() 

Nếu bạn muốn nhận được inputstream từ việc sử dụng drawable sau:

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

Vậy làm cách nào để có được hình ảnh hiện tại về hình ảnh thành chuỗi. Ví dụ: R.drawable.alright từ mã của tôi ở trên. Sử dụng imageview.getdrawable.tostring() tôi nhận được [email protected] –

+1

Mặc dù tôi chưa sử dụng, chỉ cần thử bằng cách sử dụng: resources .getResourcePackageName (resId) + '/' + resources.getResourceTypeName (resId) + '/'+ resources.getResourceEntryName (resId); – jeet

+0

nơi tôi có thể tìm thấy biến thể có thể vẽ trên chương trình này –

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

Chức năng tốt nhất của tôi

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

} 
Các vấn đề liên quan