2016-05-29 32 views
1

ich diesen Code mit einem Screenshot der Partitur zu teilen:Android Aktie Bild auf Snapchat

Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
     sharingIntent.setType("image/png"); 
     Uri image = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/sharescore.png"); 
     try { 
      // create bitmap screen capture 
      View v1 = v.getRootView(); 
      v1.setDrawingCacheEnabled(true); 
      Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
      v1.setDrawingCacheEnabled(false); 

      File imageFile = new File(image.getPath()); 

      FileOutputStream outputStream = new FileOutputStream(imageFile); 
      int quality = 100; 
      bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
      outputStream.flush(); 
      outputStream.close(); 
     } catch (Throwable e) { 
      // Several error may come out with file handling or OOM 
      e.printStackTrace(); 
     } 
     sharingIntent.putExtra(Intent.EXTRA_STREAM, image); 
     startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via))); 

Es funktioniert gut für WhatsApp Twitter usw., aber nicht für Snapchat und Instagram auch wenn ich die „imgae/jpeg "eingeben. Dies führt zu dem Fehler "ausgewähltes Bild kann nicht geöffnet werden". Wie kann ich das für Snapchat und Instagram machen?

Antwort

1

Sie Problem war, dass ich den Screenshot auf der SD-Karte gespeichert, aber Snapchat hat nicht die Erlaubnis, Dateien aus dem externen Speicher zu lesen. Ich musste das App-Verzeichnis verwenden, das nur von meiner App verwendet werden kann, und musste die Berechtigungen mit meinem eigenen Dateiprovider (com.test.fileprovider) ändern, damit Snapchat darauf zugreifen kann. Hier ist der Code:

Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
     sharingIntent.setType("image/jpg"); 
     sharingIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     File newFile = new File(getContext().getCacheDir(), "share_score.jpg"); 
     Log.d(getClass().getSimpleName(), newFile.getAbsolutePath()); 
     Uri image = FileProvider.getUriForFile(getContext(), "com.test.fileprovider", newFile); 
     try { 
      // create bitmap screen capture 
      View v1 = v.getRootView(); 
      v1.setDrawingCacheEnabled(true); 
      Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
      v1.setDrawingCacheEnabled(false); 

      FileOutputStream outputStream = new FileOutputStream(newFile); 
      int quality = 100; 
      bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
      outputStream.flush(); 
      outputStream.close(); 
     } catch (Throwable e) { 
      // Several error may come out with file handling or OOM 
      e.printStackTrace(); 
     } 
     sharingIntent.putExtra(Intent.EXTRA_STREAM, image); 
     startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via))); 
0

Im diesen Code für Instagram mit und funktioniert gut:

AssetManager assetFiles = getAssets(); 
InputStream istr = null; 
Bitmap image = null; 

try { 
    istr = assetFiles.open("img.jpg"); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

image = BitmapFactory.decodeStream(istr); 

String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), image, "Share Text", null); 
Uri bmpUri = Uri.parse(pathofBmp); 

Intent sendIntent = new Intent(); 
sendIntent.setAction(Intent.ACTION_SEND); 
sendIntent.putExtra(Intent.EXTRA_TEXT, "Share Text"); 
sendIntent.setType("image/png"); 
sendIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
sendIntent.setPackage("com.instagram.android"); 
startActivity(sendIntent); 

Ich hoffe, diese Schnipsel helfen Ihnen.

+0

Danke für Ihre Antwort, aber ich habe das Problem bereits selbst gelöst – cmtut