2015-04-06 153 views
1

我正在编写一个应用程序来共享图像。 我写了代码,但得到问题。 我在做什么 1:将图像暂时保存在内部Stoarge中 2:传递URI以共享图像。无法在Whatsap上共享图片

图像已成功保存在内部存储器中,但未获得whatsapp的共享。 当我在whatsapp上分享它时,Whatsapp会打开,我选择收件人,whatsapp处理它并说“重试”。

代码:

public void shareImage(View V) 
    { 

     Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.download); 
     Intent share = new Intent(Intent.ACTION_SEND); 
     share.setType("image/jpeg"); 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

     File file = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "myDownloadedImage.jpg"); 


    try 
    { 
     file.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(file); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (IOException e) {   
     Toast.makeText(this, "Some error in Writing"+e.getMessage(), Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
    } 

    Uri downloadLocation=Uri.fromFile(file); 
    share.putExtra(Intent.EXTRA_STREAM, downloadLocation); 
    startActivity(Intent.createChooser(share, "Share Image")); 

}

形象成功地得到保存的内部存储空间,但没有得到在WhatsApp的共享。

截图如下。我们可以看到图片不能共享成功。下面的代码

enter image description here

+0

可能重复的[与URL的Android共享意图共享图像](http://stackoverflow.com/questions/25135873/share-image-with-url-android-share-intent) – 2015-04-06 06:40:28

+0

我已经检查过,也检查了很多其他问题。当我没有上班时,我发布这个问题。 – user2446474 2015-04-06 06:45:31

回答

2

使用来获取图像,使URI:

   File file = new File(Environment.getExternalStoragePublicDirectory( 
        Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
       file.getParentFile().mkdirs(); 
       FileOutputStream out = new FileOutputStream(file); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 
       out.close(); 
       bmpUri = Uri.fromFile(file); 

现在通过bmpUri在:

shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 

这应该使你的形象份额。