2016-05-17 39 views
0

我正在開發android中的一個應用程序,在那個應用程序中,我必須根據經緯度獲得靜態地圖圖像,長期提供n與whatsapp共享文本。我正在使用以下代碼段,但它顯示共享失敗,當我嘗試在whatsapp上共享它時。在whatsapp上分享谷歌靜態地圖圖片

String whatsAppMessage = "Refer this map image"; 

    Uri uri = Uri.parse("http://maps.google.com/maps/api/staticmap?center=" + latEiffelTower + "," + lngEiffelTower + "&zoom=15&size=200x200&sensor=false"); 

    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage); 
    intent.setType("text/plain"); 
    intent.putExtra(Intent.EXTRA_STREAM,uri); 
    intent.setType("image/jpeg"); 
    intent.setPackage("com.whatsapp"); 
    startActivity(intent);** 
+0

我在鏈接上看到的圖像有* .png格式。嘗試從intent.setType(「image/jpeg」);到intent.setType(「image/png」);可能會有所幫助。 – Konstantin

+0

您的Uri的MIME類型不正確,請嘗試'intent.setType(「text/html」);' –

回答

0

試試下面的一段代碼:

private static final int IMAGE_WIDTH = 600; 
private static final int IMAGE_HEIGHT = 400; 

private void shareWhatsappImage(Uri imageUri) { 
    String pictureText = "Enter your text here"; 

    Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setPackage("com.whatsapp"); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, pictureText); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); 
    shareIntent.setType("image/jpeg"); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

    try { 
     startActivity(shareIntent); 
    } catch (android.content.ActivityNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
} 

private void getBitmap() { 
    String urlToShare = "http://maps.googleapis.com/maps/api/staticmap?center=Australia&size=" + IMAGE_WIDTH + "x" + IMAGE_HEIGHT; 

    Glide.with(getApplicationContext()) 
      .load(urlToShare) 
      .asBitmap() 
      .into(new SimpleTarget<Bitmap>(IMAGE_WIDTH, IMAGE_HEIGHT) { 
       @Override 
       public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { 
        shareWhatsappImage(getImageUri(MainActivity.this, resource)); 
       } 

       @Override 
       public void onLoadFailed(Exception e, Drawable errorDrawable) { 
        super.onLoadFailed(e, errorDrawable); 
       } 
      }); 
} 

public Uri getImageUri(Context inContext, Bitmap inImage) { 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "DemoImage", null); 
    return Uri.parse(path); 
} 

只要打電話getBitmap(),你應該能夠共享文本和圖像到WhatsApp的。

請確保您有:

AndroidManifest.xml

而且

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

你可能需要處理WRITE_EXTERNAL_STORAGE權限的Android M.

參考文獻:

Download Bitmap from Glide
Share Image & Text to Whatsapp
Get Uri from Bitmap