Android 共享映像失败
我正试图分享一张Android 共享映像失败,android,image,android-intent,sharing,Android,Image,Android Intent,Sharing,我正试图分享一张图片,但我不知道为什么我失败了,你能帮我吗 String imageUrl = web.get(position).getImage(); if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://")) imageUrl = "http://" + imageUrl; Button button = (Button)rowView.findViewB
图片
,但我不知道为什么我失败了,你能帮我吗
String imageUrl = web.get(position).getImage();
if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
imageUrl = "http://" + imageUrl;
Button button = (Button)rowView.findViewById(R.id.condividi);
final String finalImageUrl = imageUrl;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
File file = writebitmaptofilefirst("the image", finalImageUrl );
Uri path = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, path );
Intent send = Intent.createChooser(intent, null);
context.startActivity(send);
}
});
编辑
String imageUrl = web.get(position).getImage();
if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
imageUrl = "http://" + imageUrl;
Button button = (Button)rowView.findViewById(R.id.condividi);
final String finalImageUrl = imageUrl;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
String file = writebitmaptofilefirst("ndp_image", finalImageUrl);
//Uri path = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, file );
Intent send = Intent.createChooser(intent, null);
context.startActivity(send);
}
});
return rowView;
}
public static String writebitmaptofilefirst(String filename, String source) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extStorageDirectory + "/temp_images/");
if (!mFolder.exists()) {
mFolder.mkdir();
}
OutputStream outStream = null;
File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".jpg");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
URL url = new URL(source);
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file.getAbsolutePath();
}
向清单中添加权限 从文件名(图像)中删除空间。您需要使用空格来解码uri。您应该访问完整路径返回文件。getAbsolutePath()。您正在传递文件名。
在文件存在的情况下,您的存储路径不相同。字典里没有包括你。extranlstoragepath+/temp_images/+the image.jpg尝试记录文件路径。及 File File=新文件(mFolder.getAbsolutePath(),filename+“.jpg”) 你错过了两个参数之间的一个
您遇到了什么问题?撞车?如果是,请张贴日志too@user2340612我没有崩溃,当我准备使用社交应用程序发送文件时,只会出现一条消息,如:共享失败,请重试(对于Whatsapp)或无法上传图像(对于Instagram)您确定需要
EXTRA_文本
和EXTRA_流
EXTRA吗?我想你只需要第二个,但我需要与图像共享一些文本也许可以帮助你我尝试放置返回文件。getAbsolutePath()但什么都没有你得到了什么?图像保存到存储?你添加权限了吗?我简直不敢相信:我添加的代码比我以前的代码好
String imageUrl = web.get(position).getImage();
if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
imageUrl = "http://" + imageUrl;
Button button = (Button)rowView.findViewById(R.id.condividi);
final String finalImageUrl = imageUrl;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
String file = writebitmaptofilefirst("ndp_image", finalImageUrl);
//Uri path = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, file );
Intent send = Intent.createChooser(intent, null);
context.startActivity(send);
}
});
return rowView;
}
public static String writebitmaptofilefirst(String filename, String source) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extStorageDirectory + "/temp_images/");
if (!mFolder.exists()) {
mFolder.mkdir();
}
OutputStream outStream = null;
File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".jpg");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
URL url = new URL(source);
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file.getAbsolutePath();
}