通过我的应用程序发送图像到whatsapp

问题描述:

我每个人,我有一个问题。我必须从我的应用程序发送图像到whatsapp。我以位图格式下载了图像。在以JPG格式转换位图格式后,我尝试发送图像。但WhatsApp没有收到任何数据。通过我的应用程序发送图像到whatsapp

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.setType("image/jpeg"); 

Bitmap bmp = getBitmap(); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream); 

File f = new File(getAppContext().getCacheDir() + File.separator + "temporary_file.jpg"); 
try { 
     f.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(stream.toByteArray()); 
     fo.flush(); 
     fo.close(); 
} catch (IOException e) { 
     e.printStackTrace(); 
} 

shareIntent.putExtra(Intent.EXTRA_STREAM, f.getPath()); 
myShareActionProvider.setShareIntent(shareIntent); 

我已经插入到清单中的所有权限。 我该如何解决? 谢谢。

PS。我使用min sdk 17和max sdk 24

+0

https://*.com/a/44639312/115145 – CommonsWare

WhatsApp无法访问getCacheDir()返回的目录,只有您的应用程序可以。尝试做这样的代替:

File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"); 

对于这个工作,你需要声明WRITE_EXTERNAL_STORAGE许可,您的manifest.xml文件。就像这样:

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

我试图创建一个使用Environment.getExtrnalStorageDirectory()的文件,但是当我尝试createNewFile()返回IOException的权限被拒绝 – apollo9

+0

因为您需要将WRITE_EXTERNAL_STORAGE权限添加到manifest.xml文件中,所以我将编辑答案。 – Tharkius