我想使用自己的应用程序将图像共享到所有“ Instagram ”,“ WhatsApp ”,或者也许使用任何社交媒体应用程序?
当我获得了whatsapp的解决方案时,我仍然无法获得Instagram的解决方案。
我的代码如下:
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp_"+n+".jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
String savedFile = saveImageFile(bitmap, "myFolder");
Uri imageUri = Uri.parse(savedFile);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Share Image"));
}
});
}
savedImages方法:-
public static String saveImageFile(Bitmap image, String folder){
String now = Long.toString(new Date().getTime());
File imageFile = new File(Environment.getExternalStorageDirectory()+"/" + folder);
if (!imageFile.exists()){
File screenShotsFolder = new File(Environment.getExternalStorageDirectory()+"/"+ folder+ "/");
screenShotsFolder.mkdirs();
}
File imageName = new File(new File(Environment.getExternalStorageDirectory() +"/"+ folder + "/"), now + ".jpg" );
try{
FileOutputStream outputStream = new FileOutputStream(imageName);
image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
}catch (Throwable e){e.printStackTrace();}
return imageName.toString();
}
此代码适用于“ WhatsApp ”,但不适用于“ Instagram ”
在Instagram中,我无法发送给诸如“ 9gag ”之类的特定人。
答案 0 :(得分:3)
使用
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(f.getPath()));
代替Environment.getExternalStorageDirectory().getPath())
但是对于牛轧糖及以上,您应该使用FileProvider
来获取文件uri。
picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", f);
答案 1 :(得分:2)
这是完整的代码。将您的代码更改为以下
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String savedFile = saveImageFile(bitmap, "myFolder");
Uri imageUri = Uri.parse(savedFile);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Share Image"));
}
});
保存图像文件功能
public static String saveImageFile(Bitmap image, String folder){
String now = Long.toString(new Date().getTime());
File imageFile = new File(Environment.getExternalStorageDirectory() + folder);
if (!imageFile.exists()){
File screenShotsFolder = new File("/sdcard/Pictures/" + folder+ "/");
screenShotsFolder.mkdirs();
}
File imageName = new File(new File("/sdcard/Pictures/" + folder + "/"), now + ".jpg" );
try{
FileOutputStream outputStream = new FileOutputStream(imageName);
image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
}catch (Throwable e){e.printStackTrace();}
return imageName.toString();
}
答案 2 :(得分:1)