2015-01-08 59 views
0

我的問題如何發送位圖到Whastapp應用程序,我使用下面的代碼;如何發送位圖到WhatsApp應用程序

ImageView iv=(ImageView)view.findViewById(R.id.item_image); 
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap(); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA); 
//Check if package exists or not. If not then code 
//in catch block will be called 
waIntent.setPackage("com.whatsapp"); 
waIntent.setType("image/png"); 
waIntent.putExtra(Intent.ACTION_SEND, byteArray); 
startActivity(Intent.createChooser(waIntent, "Share with")); 

但是該代碼沒有工作。我的錯誤是什麼?謝謝。

+0

你可以把'Bitmap'直接進入'Intent'的'Bundle'使用'putParcelable()' –

+0

@abforce可以請您給一個例子 – Ozan

+0

http://stackoverflow.com/questions/2459524/how-can-i-pass-a-bitmap-object-from-one-activity-to-another –

回答

1

這爲我工作:

public void onClickApp(String pack, Bitmap bitmap) { 
    PackageManager pm = context.getPackageManager(); 
    try { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null); 
     Uri imageUri = Uri.parse(path); 

     @SuppressWarnings("unused") 
     PackageInfo info = pm.getPackageInfo(pack, PackageManager.GET_META_DATA); 

     Intent waIntent = new Intent(Intent.ACTION_SEND); 
     waIntent.setType("image/*"); 
     waIntent.setPackage(pack); 
     waIntent.putExtra(android.content.Intent.EXTRA_STREAM, imageUri); 
     waIntent.putExtra(Intent.EXTRA_TEXT, pack); 
     context.startActivity(Intent.createChooser(waIntent, "Share with")); 
    } catch (Exception e) { 
     Log.e("Error on sharing", e + " "); 
     Toast.makeText(context, "App not Installed", Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

你總是使用正常代碼的代碼片段。代碼片段只能用於HTML或JavaScript或其他可以在瀏覽器中運行的代碼。您無法在瀏覽器中運行Java。在未來使用正常的代碼塊...我將爲您編輯您的答案,並修復格式等,但請不要在將來再這樣做。這不是我第一次告訴你這個... –

+0

這需要writeexternalstorage權限,如何在沒有 –

-1
//pass your image and text(if you want to share) in this method. 
    void shareImage(Bitmap bitmap,String text){ 
    //bitmap is ur image and text is which is written in edtitext 
    //you will get the image from the path 
    String pathofBmp= 
    MediaStore.Images.Media.insertImage(getContentResolver(), 
    bitmap,"title", null);  
    Uri uri = Uri.parse(pathofBmp); 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.setType("image/*"); 
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Star App"); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, text); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    startActivity(Intent.createChooser(shareIntent, "hello hello")); 
} 
相關問題