分享图片时出错
Error With sharing image
我想使用 ACTION_SEND 分享图片。所以基本上当用户点击图像并选择 "share image" 时,它应该发送所选图像,
因此,在测试时,我需要使用哪个应用程序来与WhatsApp,Facebook,电子邮件等共享,然后选择任何一个应用程序,然后说"sharing failed, please try again."我似乎无法弄清楚为什么不起作用。但是,我有相同的代码来使用 ACTION_VIEW 全屏显示图像文件,这似乎很好用,但不适用于共享。
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
第一步:先添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第 2 步:编辑
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imagePath);
设置类型image/jpg而不是图片/*
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
这对我有用
首先将我的图像存储在外部存储器中
private void SaveImage(Bitmap finalBitmap) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = "image.jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
}
}
## 。 ##
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ahmed);
SaveImage(img);
}
然后从外部存储加载我的图像然后共享它。
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/saved_images/image.jpg"));
startActivity(Intent.createChooser(share, "Share image using"));
}
注意
从模拟器获取路径
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: Scanned /storage/emulated/0/saved_images/image.jpg:
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: -> uri=content://media/external/images/media/106290
我想使用 ACTION_SEND 分享图片。所以基本上当用户点击图像并选择 "share image" 时,它应该发送所选图像, 因此,在测试时,我需要使用哪个应用程序来与WhatsApp,Facebook,电子邮件等共享,然后选择任何一个应用程序,然后说"sharing failed, please try again."我似乎无法弄清楚为什么不起作用。但是,我有相同的代码来使用 ACTION_VIEW 全屏显示图像文件,这似乎很好用,但不适用于共享。
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
第一步:先添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第 2 步:编辑
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imagePath);
设置类型image/jpg而不是图片/*
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
这对我有用
首先将我的图像存储在外部存储器中
private void SaveImage(Bitmap finalBitmap) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = "image.jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
}
}
## 。 ##
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ahmed);
SaveImage(img);
}
然后从外部存储加载我的图像然后共享它。
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/saved_images/image.jpg"));
startActivity(Intent.createChooser(share, "Share image using"));
}
注意
从模拟器获取路径
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: Scanned /storage/emulated/0/saved_images/image.jpg:
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: -> uri=content://media/external/images/media/106290