简体   繁体   English

如何在android中意图共享图像位图?

[英]how to share a image bitmap with intent in android?

(This question isn't duplicate !) (这个问题不是重复的!)

How can I share an image via intent ? 如何通过意图共享图像?

I tried this 我试过了

Android Share Intent for a Bitmap - is it possible not to save it prior sharing? 位图的Android共享意图-是否可以在共享之前不保存它?

How to share image to social media with bitmap? 如何使用位图将图像分享到社交媒体?

share image with URL android share intent 使用URL android共享意图共享图像

but only worked on the emulator and did not work on the actual phone and made the following code error: 但仅在仿真器上可用,而在实际的电话上则不可用,并导致以下代码错误:

//b method : convert inputstream to bitmap
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), b(inputStream) ,"title", null);

Full code : 完整代码:

InputStream is = getAssets().open(imageName.getText().toString());
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String path = Environment.getExternalStorageDirectory()+"/" + APP_NAME()+"/pictures/"+ls+"/" ;
new File(path).mkdirs();
String fileName = System.currentTimeMillis() + imageName.getText().toString().replace("pic/" , "");
FileOutputStream fileOutputStream = new FileOutputStream(new File(path+fileName));
fileOutputStream.write(buffer);
fileOutputStream.close();
InputStream inputStream = new FileInputStream(new File(path+fileName));

String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), b(inputStream) ,"title", null);
Uri bitmapUri = Uri.parse(bitmapPath);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM , bitmapUri);
startActivity(intent);


//Intent intent = new Intent(android.content.Intent.ACTION_SEND);
//intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
//intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.setType("image/*");
//startActivity(intent);

My problem was solved with a small change in my code: 我的问题通过对我的代码进行了小的更改就解决了:

Uri bitmapUri = Uri.parse (new File (path + fileName) .toString ());

Full code : 完整代码:

InputStream is = getAssets().open(imageName.getText().toString());
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String path = Environment.getExternalStorageDirectory()+"/" + APP_NAME()+"/pictures/"+ls+"/" ;
new File(path).mkdirs();
String fileName = System.currentTimeMillis() + imageName.getText().toString().replace("pic/" , "");
FileOutputStream fileOutputStream = new FileOutputStream(new File(path+fileName));
fileOutputStream.write(buffer);
fileOutputStream.close();
//InputStream inputStream = new FileInputStream(new File(path+fileName));

//String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(),b(inputStream) ,"title", null); // comment this line
Uri bitmapUri = Uri.parse(new File(path+fileName).toString()); //changed

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM , bitmapUri);
startActivity(intent);

if (new File(path+fileName).exists()){
    new File(path+fileName).delete();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM