2016-03-24 22 views
0

Benim çalışmamda bir ImageView var. Kullanıcı bunu tıkladığında, görüntüyü açabilen uygulamaların listesini gösteren bir iletişim kutusu (amaç iletişim kutuları gibi), bir uygulamayı seçip görüntüyü o uygulamayla gösterebileceğinden istiyorum.Android: Diğer uygulamalar ile paylaşılabilir kaynak paylaşın

Etkinliğimi kodu: - Bir niyetine bir bit eşlem geçemez

  Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_VIEW); 
      intent.setDataAndType(<your_image_uri>, "image/*"); 
      startActivity(intent); 

cevap

2

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ImageView iv = (ImageView) findViewById(R.id.imageid); 
    iv.setImageResource(R.drawable.dish); 
    iv.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //here is where I want a dialog that I mentioned show 
       } 
    }); 
}// end onCreate() 
-1

O türü Intent.ACTION_VIEW niyetini kullanarak startActivity gerekiyor.

Gördüğüm kadarıyla, kaynaklarınızdan çekilebilir bir şekilde paylaşmak istiyorsunuz. Bu yüzden ilk önce çekilebilir bir bitmap dönüştürmek zorunda. Ve sonra bitmap'i bir dosya olarak harici belleğe kaydetmeniz ve daha sonra Uri.fromFile (yeni Dosya (pathToTheSavedPicture)) kullanarak bu dosya için bir uri almanız ve bu uri'yi bu gibi niyetle geçirmeniz gerekir.

shareDrawable(this, R.drawable.dish, "myfilename"); 

public void shareDrawable(Context context,int resourceId,String fileName) { 
    try { 
     //convert drawable resource to bitmap 
     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId); 

     //save bitmap to app cache folder 
     File outputFile = new File(context.getCacheDir(), fileName + ".png"); 
     FileOutputStream outPutStream = new FileOutputStream(outputFile); 
     bitmap.compress(CompressFormat.PNG, 100, outPutStream); 
     outPutStream.flush(); 
     outPutStream.close(); 
     outputFile.setReadable(true, false); 

     //share file 
     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile)); 
     shareIntent.setType("image/png"); 
     context.startActivity(shareIntent); 
    } 
    catch (Exception e) { Toast.makeText(context, "error", Toast.LENGTH_LONG).show(); 
    } 
} 
-1
Create a chooser by using the following code. You can add it in the part where you say imageview.setonclicklistener(). 
Intent intent = new Intent(); 
// Show only images, no videos or anything else 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
// Always show the chooser (if there are multiple options available) 
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1); 
İlgili konular