2011-11-24 55 views
0

我正在创建一个照片应用程序,用户从该图库中选择图像并将其显示在应用程序中心的图像视图中。我将如何去创建ImageView的共享意图?如何为ImageView创建共享意图?

UPDATE 分享意图正在工作,但是,图像无法共享,因为我无法将其保存到我选择的路径。以下是我的代码。任何帮助?

public void taptoshare(View v) 
    { 
     View content = findViewById(R.id.myimage); 
     content.setDrawingCacheEnabled(true); 
      Bitmap bitmap = content.getDrawingCache(); 
      File file = new File("/DCIM/Camera/image.jpg"); 
      try 
      { 
       file.createNewFile(); 
       FileOutputStream ostream = new FileOutputStream(file); 
       bitmap.compress(CompressFormat.JPEG, 100, ostream); 
       ostream.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 


     Intent shareIntent = new Intent(Intent.ACTION_SEND); 
     Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg"); 
     shareIntent.setData(phototUri); 
     shareIntent.setType("image/*"); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri); 
     startActivity(Intent.createChooser(shareIntent, "Share Via")); 

} 

}

+0

你要共享的图像或想从原生图库中挑选图片? –

+0

我想分享在ImageView中显示的图像(用户从图库中选择的一张照片) – dabious

回答

1

试试这个

Intent shareIntent = new Intent(Intent.ACTION_SEND); 
Uri phototUri = Uri.parse(path); 
shareIntent.setData(photootUri); 
shareIntent.setType("image/png"); 
shareIntent.putExtra(Intent.EXTRA_STREAM, photootUri); 
getContext().startActivity(Intent.createChooser(shareIntent, "Use this for sharing")); 
+0

分享意图很好用!但是,如何将图像保存到磁盘中?弹出菜单和一切共享,但因为找不到指定的路径而失败。 – dabious

+0

您需要保存相机应用程序中的图像。然后,您可以在共享意图中传递该文件路径URI。 http://www.technotalkative.com/android-pick-image-from-native-gallery/ – Gopinath