2015-04-06 134 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); 

這應該使你的形象份額。