Java 无法将图像发送到其他应用程序,已尝试hike、Messenger和Whatsapp
我试图将我的应用程序中的图像共享到其他应用程序(Hike、Facebook、Messenger等),但每次都会出现不同的错误。几乎每一个问答都经过了,但问题还没有解决 这是我的代码:Java 无法将图像发送到其他应用程序,已尝试hike、Messenger和Whatsapp,java,android,android-intent,sharing,Java,Android,Android Intent,Sharing,我试图将我的应用程序中的图像共享到其他应用程序(Hike、Facebook、Messenger等),但每次都会出现不同的错误。几乎每一个问答都经过了,但问题还没有解决 这是我的代码: filepath = Environment.getExternalStorageDirectory(); cacheDir = new File(filepath.getAbsolutePath()
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);
1. intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse(str1));
2. intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file1);
我多次更改了下面的行,但没有得到解决方案:
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
.getAbsolutePath())));
我已经用改变了这一点:
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);
1. intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse(str1));
2. intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file1);
但是没有得到所需要的。而且,当我向Whatsapp发送图像时,它不会显示图像,而在发送后,它会显示
尝试下面的代码,这些代码在我的应用程序中运行良好
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::您需要为WhatsAPP(或任何其他应用程序)启用读/写权限才能使用图像。创建一个临时文件,或让应用程序将文件保存到外部存储器。
- 本教程帮助了我:
- 这个android开发者链接向我解释了为什么我必须选择
路径:getExternalStorageDirectory()
Uri
对象相关的任何内容):intent.addFlags(intent.FLAG\u GRANT\u READ\u Uri\u PERMISSION)代码>
也不是来自FileOutputStream fos=context.openFileOutput(“desiredFilename.png”,context.MODE\u PRIVATE)的以下内容代码>
然后将Context.MODE\u PRIVATE
更改为Context.MODE\u WORLD\u READABLE
。。。它在API-14之后被弃用
这部分没有任何问题:
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())));
这部分工作得很好,因为它启动了WhatsApp过程,并等待您添加标题。通过添加intent.putExtra(intent.EXTRA\u TEXT,TEXT\u string)
它将获取text\u string
中找到的任何字符串值,并将其添加为图片标题。不幸的是,intent.putExtra()
被WhatsAPP接收/理解,只有一张图片和一个标题。。。我不知道如何使它与多个标题的多张图片
另一个提示:如果错误输出显示文件目录不存在
,可能是因为行mkdirs()
不起作用。本教程使用两个选定的目录APP\u PATH\u SD\u CARD
和APP\u缩略图\u PATH\u SD\u CARD
——并要求mkdirs()
同时创建这两个目录。。。出于某种原因,mkdirs()
不喜欢这样做。
所以我要求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
希望这些链接能帮助其他遇到同样问题的人。:) 这里的“描述”和“描述”是什么?在我执行此操作后变红,在我的codedescpic中是imageview和DescTextView,用您的文本更改它您面临什么问题?您尝试过我的答案吗?