2017-10-25 2 views
0

So versuche ich Intent.ACTION_SEND zu verwenden, um ein Bild von meiner App zu teilen.Bild mit Aktion teilen senden

Dies ist der Code, den ich

Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, shareImgURI); 
    shareIntent.setType("image/*"); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    startActivity(Intent.createChooser(shareIntent, "Share images...")); 

und die shareImgURI ist content://com.android.providers.media.documents/document/image%3A298

Es öffnet sich die Auswahl verwende, aber wenn ich eine Option, es passiert nichts aus. Fehle ich etwas?

+0

Wenn shareImgURI String-Typ ist Dann tun Uri.parse (shareImgURI) –

+0

der shareImgURI ist vom Typ Uri – pavlos

+0

Welche API verwendet? Wenn> 25 Sie Provider für den Zugriff auf SD-Card-Inhalt erstellen sollten –

Antwort

1

Versuchen Sie, diese

Bitmap icon = mBitmap; 
Intent share = new Intent(Intent.ACTION_SEND); 
share.setType("image/jpeg"); 
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"); 
try { 
    f.createNewFile(); 
    FileOutputStream fo = new FileOutputStream(f); 
    fo.write(bytes.toByteArray()); 
} catch (IOException e) {      
     e.printStackTrace(); 
} 
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg")); 
startActivity(Intent.createChooser(share, "Share Image")); 
+0

Danke klappt wunderbar! – pavlos

0

Verwenden Sie den Code unten

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
@SuppressWarnings("deprecation") 
public static Intent shareImage(Context context, String pathToImage) { 
Intent shareIntent = new Intent(Intent.ACTION_SEND); 
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) 
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
else 
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); 

shareIntent.setType("image/*"); 

// For a file in shared storage. For data in private storage, use a ContentProvider. 
Uri uri = Uri.fromFile(context.getFileStreamPath(pathToImage)); 
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
return shareIntent;} 
0

Code ändern zu diesem

Intent shareIntent = new Intent(Intent.ACTION_SEND); 
shareIntent.setType("image/jepg"); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+shareImgURI) 
startActivity(Intent.createChooser(shareIntent, "Share image")); 

Fügen Sie diesen Code in Ihre Aktivität onCreate()

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); 
StrictMode.setVmPolicy(builder.build()); 
+0

Warum verwenden Sie 'Uri.parse (" file: // "+ shareImgURI)' wenn 'shareImgURI' bereits vom Typ' Uri' ist? – pavlos

+0

Dann, wenn Sie die 'stringUri' auf' Uri Variable' analysieren, fügen Sie 'file: //' hinzu und der Pfad sollte der absolute Pfad zum Bild sein wie '/ storage/emulated/0/Pictures/image.jpg' @pavlos –

0
Bitmap icon = mBitmap; 
Intent share = new Intent(Intent.ACTION_SEND); 
share.setType("image/jpeg"); 
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
File f = new File(Environment.getExternalStorageDirectory() + File.separator 
+ "temporary_file.jpg"); 
try { 
f.createNewFile(); 
FileOutputStream fo = new FileOutputStream(f); 
fo.write(bytes.toByteArray()); 
} catch (IOException e) {      
    e.printStackTrace(); 
} 
share.putExtra(Intent.EXTRA_STREAM, 
Uri.parse("file:///sdcard/temporary_file.jpg")); 
startActivity(Intent.createChooser(share, "Share Image")); 

Versuchen Sie es, es wird funktionieren.