[英]Unable to send images to other apps, tried hike, Messenger and Whatsapp
I am trying to share images from my app to other app (Hike, Facebook, Messenger etc) but every time I am getting different error. 我试图将图像从我的应用程序共享到其他应用程序(远足,Facebook,Messenger等),但是每次遇到不同的错误时,我都会尝试共享。 Gone through almost every Q&A but problem not solved yet.
几乎完成了所有问答,但问题尚未解决。
This is my code : 这是我的代码 :
filepath = Environment.getExternalStorageDirectory();
cacheDir = new File(filepath.getAbsolutePath()
+ "/LikeIT/");
cacheDir.mkdirs();
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
.getAbsolutePath())));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
I have changed the below line many times, but didn't get the solution: 我已经更改了以下几行,但是没有找到解决方案:
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
.getAbsolutePath())));
I have changed this with : 我已经用 :
1. intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse(str1));
2. intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file1);
but didn't get as needed. 但没有得到所需的。 And also when i am sending image to Whatsapp it is not showing image, and after sending it is showing.
而且,当我将图像发送到Whatsapp时,它没有显示图像,并且在发送图像后也显示了图像。
Try below code that works fine in my app 请尝试以下在我的应用中正常运行的代码
public void onShareItem(View v) {
// Get access to bitmap image from view
// Get access to the URI for the bitmap
Uri bmpUri = getLocalBitmapUri(descpic);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, desc.getText().toString());
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
Log.e("check for intent", "Couldn't get anything");
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
imageView.buildDrawingCache();
Bitmap bm = imageView.getDrawingCache();
OutputStream fOut = null;
Uri outputFileUri=null;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File imageFile = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(imageFile);
fOut = new FileOutputStream(imageFile);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
return outputFileUri;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
TL;DR ::: You need to enable read/write permissions for WhatsAPP (or any other APP) to use the images. TL; DR :::您需要为WhatsAPP(或任何其他APP)启用读/写权限才能使用图像。 Create a temporary file --- OR --- make your APP save files to external storage.
创建一个临时文件---或---使您的APP将文件保存到外部存储。
getExternalStorageDirectory()
route : Develop > Training > Getting > Started > Saving Data getExternalStorageDirectory()
路线:“ 开发”>“培训”>“入门”>“开始”>“保存数据” This is exactly what happened to me and my app. 这正是我和我的应用程序发生的事情。 Took me a day of fiddling around, and I've finally resolved it.
花了我一天的时间摆弄,我终于解决了。
No matter what, this line of code did not work (or anything relating to modifying the Uri
object ) : intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
无论如何,这行代码不起作用(或与修改
Uri
对象有关的任何事情): intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Nor the following from FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
的以下内容也不是FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
then changing Context.MODE_PRIVATE
to Context.MODE_WORLD_READABLE
... which was deprecated after API-14 然后将
Context.MODE_PRIVATE
更改为Context.MODE_WORLD_READABLE
...,在API-14之后已弃用
It's not anything wrong with this part: 这部分没有什么问题:
filepath = Environment.getExternalStorageDirectory();
cacheDir = new File(filepath.getAbsolutePath()
+ "/LikeIT/");
cacheDir.mkdirs();
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
.getAbsolutePath())));
That part is working just fine, since it starts the WhatsApp process --- and waits for you to add a caption. 该部分工作正常,因为它启动了WhatsApp流程---等待您添加字幕。 By adding
intent.putExtra(Intent.EXTRA_TEXT, text_string);
通过添加
intent.putExtra(Intent.EXTRA_TEXT, text_string);
it'll take whatever string values found in text_string
and adds it as the pictures caption. 它会采用在
text_string
找到的任何字符串值,并将其添加为图片标题。 Unfortunately, intent.putExtra()
is received/understood by WhatsAPP with just one picture with one caption ... I'm not sure how to make it multiple pictures with multiple captions... 不幸的是,
intent.putExtra()
仅通过一张带有一个标题的图片来接收/理解了intent.putExtra()
。我不确定如何使它变成多张带有多个标题的图片...
Another tip: In case the error output says File(s) Directory doesn't exist
, it's likely due to the fact that the line mkdirs()
did not work. 另一个提示:万一错误输出显示
File(s) Directory doesn't exist
,则可能是由于mkdirs()
行mkdirs()
。 The tutorial uses two chosen directories APP_PATH_SD_CARD
and APP_THUMBNAIL_PATH_SD_CARD
--- and asks mkdirs()
to create both of them at the same time... which for some reason, mkdirs()
does not like to do. 本教程使用两个选定的目录
APP_PATH_SD_CARD
和APP_THUMBNAIL_PATH_SD_CARD
---并要求mkdirs()
同时创建两个目录...由于某种原因, mkdirs()
不喜欢这样做。 So I asked mkdirs()
to create them one at a time: 所以我要求
mkdirs()
创建一个:
File dir = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD );
if (!dir.exists()) { dir.mkdirs();}
File dirHoldingImages = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD + APP_THUMBNAIL_PATH_SD_CARD);
if (!dirHoldingImages.exists()) { dirHoldingImages.mkdirs(); }
// carry-on with the rest of the saveFileToExternalStorage code
Hope those links helps others that stumble across the same issues. 希望这些链接对遇到相同问题的其他人有所帮助。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.