2017-04-27 375 views
1
 //Share image to all 
     share = (Button)findViewById(R.id.facebook_app_id); 
     share.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Uri imageUri = Uri.parse("android.resource://" + getPackageName() +"/drawable/"+imageRes); 
       Intent intent = new Intent(Intent.ACTION_SEND); 
       intent.setType("image/*"); 

       intent.putExtra(Intent.EXTRA_STREAM, imageUri); 
       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
       startActivity(Intent.createChooser(intent , "Share")); 


      } 

     }); 

我正在嘗試構建照片共享應用程序。臉譜,信使,Skype的工作完美,但的WhatsApp和Viber的顯示錯誤(的文件格式不支持) :-(不支持文件格式 - 錯誤顯示當我在whatsapp上發送照片

我在Android的初學者。我想很多,但沒有工作。請請請幫我

+0

試試這個:String path = MediaStore.Images.Media.insertImage(getContentResolver(), 「YOUR_PATH」,「Image Description」,null); Uri uri = Uri.parse(path); –

+0

什麼是「YOUR_PATH」?你的意思是<<「android.resource://」+ getPackageName()+「/ drawable /」+ imageRes >>? –

+0

根據我的代碼是我的圖像路徑是=「android.resource://」+ getPackageName()+「/ drawable /」+ imageRes和什麼是圖像描述?@Divyesh Patel –

回答

0

我不知道,但你可以檢查 imageRes與有擴展名如.JPEG或png 例如 android.resource:// 「+ getPackageName()+ 」/繪製/「 +」 imageRes.jpg「

0

嘗試設置內容類型e股意爲image/png。正如你上面所說,這可能會或可能不適用於所有應用程序。

的原因是從你的應用程式的內部存儲(當然你的應用程序的源泉將永遠在內部存儲)

有實現這一目標的兩種方法不能直接共享一個URI ..

將圖像複製到外部存儲器,然後從那裏共享。看到這個

編寫一個內容提供商共享圖片。對於從內部存儲是指創建和共享文件

+0

你可以給我任何參考共享圖像到外部存儲和內容提供商共享圖像 –

+0

http://stackoverflow.com/questions/12170386/create-and-share-a-file-from-internal-storage – albeee

0

下面是一個例子,如何做到這一點:

public void shareImageWhatsApp() { 

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv); 
    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("image/jpeg"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    File f = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "temporary_file.jpg"); 
    try { 
     f.createNewFile(); 
     new FileOutputStream(f).write(bytes.toByteArray()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    share.putExtra(Intent.EXTRA_STREAM, 
      Uri.parse(Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg")); 
    if(isPackageInstalled("com.whatsapp",this)){ 
      share.setPackage("com.whatsapp"); 
      startActivity(Intent.createChooser(share, "Share Image")); 

    }else{ 

     Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show(); 
    } 

} 

private boolean isPackageInstalled(String packagename, Context context) { 
    PackageManager pm = context.getPackageManager(); 
    try { 
     pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES); 
     return true; 
    } catch (NameNotFoundException e) { 
     return false; 
    } 
} 
+0

很糟糕將png文件轉換爲位圖,然後將其壓縮爲流並使用第二個流寫入jpg文件。你可以將drawable直接複製到文件中。 – greenapps

0

試試這個:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_round); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("image/*"); 
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "ic_launcher_round", null); 
    Uri imageUri = Uri.parse(path); 
    intent.putExtra(Intent.EXTRA_STREAM, imageUri); 
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    startActivity(Intent.createChooser(intent , "Share")); 

此代碼之前執行確保您有給EXTERNAL_STORAGE權限和目標,並編譯sdk比lolipop更高,那麼你必須請求權限

+0

確保您的圖像在任何可繪製目錄中不是Minmap –

+0

我使用ImageView ... ImageView image =(ImageView)findViewById(R.id.imageView2); image.setImageResource(imageRes);如果我使用ImageAdapter,它工作嗎?根據我的代碼「ic_launcher_round」=?什麼是我的ic_launcher_round? –

+0

您的圖像名稱在繪圖目錄中 –

相關問題