我想通过Intent与json图像分享图像到Instagram,但我需要解析我的json图像这里是我的代码
private void createInstagramIntent(String type, String imageUrl ,String captionText){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(type);
URL url = null;
Bitmap bmp1 = null;
byte[] byteArray = null;
try {
url = new URL(imageUrl);
bmp1 = BitmapFactory.decodeStream(url.openConnection().getInputStream());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp1.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,byteArray);
share.putExtra(Intent.EXTRA_TEXT,captionText);
share.setPackage("com.instagram.android");
startActivity(Intent.createChooser(share, "Share to"));
}
答案 0 :(得分:0)
您不应该在UI线程中进行网络操作。将您的代码放到另一个帖子中,例如AsyncTask
// move following operation to another thread
bmp1 = BitmapFactory.decodeStream(url.openConnection().getInputStream());
检索图像后,将共享意图Intent.EXTRA_STREAM
设置为图像文件的Uri。以下是Instagram official document的代码副本。
String type = "video/*";
String filename = "/myVideo.mp4";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
String captionText = "<< media caption >>";
createInstagramIntent(type, mediaPath, captionText);
private void createInstagramIntent(String type, String mediaPath, String caption){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}