2016-05-17 7 views
0

ich entwickle eine anwendung in android, in dieser anwendung muss ich die statische karte bild nach lat n lang vorausgesetzt n teilen sie auf whatsapp mit text. Ich verwende das folgende Code-Snippet, aber es zeigt die Freigabe fehlgeschlagen, wenn ich versuche, es auf whatsapp zu teilen.Share google static map image auf 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

Bild, das ich auf dem Link sehe, habe * .png Format. versuche, von intent.setType ("image/jpeg") zu wechseln; zu intent.setType ("image/png"); vielleicht hilft es. – Konstantin

+0

Ihr MIME-Typ für den URI ist falsch, versuchen Sie 'intent.setType (" text/html ");' –

Antwort

0

das folgende Stück Code Versuchen:

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); 
} 

Nur getBitmap() nennen und Sie sollten den Text der Lage sein und Bild zu teilen beide WhatsApp.

Händigen Sie:

Download Bitmap from Glide,
Share Image & Text to Whatsapp und:

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

in AndroidManifest.xml

Auch Sie könnten WRITE_EXTERNAL_STORAGE Erlaubnis für Android M.

Referenzen behandeln müssen
Get Uri from Bitmap