Java 在android中共享图像时出错
在android中,我不能以jpeg格式和png格式共享图像。请帮助并更正我的代码 每当我为共享编码时,它就会出现异常“不支持文件格式”Java 在android中共享图像时出错,java,android,image,share,Java,Android,Image,Share,在android中,我不能以jpeg格式和png格式共享图像。请帮助并更正我的代码 每当我为共享编码时,它就会出现异常“不支持文件格式” 这是我的代码: Uri imageUri = Uri.parse("android.resource://com.parekh.shareimage/drawable"); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shar
这是我的代码:
Uri imageUri = Uri.parse("android.resource://com.parekh.shareimage/drawable");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(shareIntent);
将可绘制图像存储到内部存储器中,然后选择该图像。这样对你有帮助
try{
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.shareforma);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "forma.PNG");
FileOutputStream outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
String msgText = "Sample Message";
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
//set your message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText);
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, msgText));
这段代码指向了一个方向,除此之外,相对较少的应用程序能够很好地处理
android.resource
方案。而且,并不是每个应用程序都能同时处理EXTRA_TEXT
和EXTRA_STREAM
。请更正我的代码,这取决于你从哪里获取图像我是android新手,你能给我一个链接或网站吗?我可以从哪里学习?如果我有大约50幅图像,那么我如何通过R.drawable.shareforma???需要帮助对于单个图像,此方法将是合适的。对于多个图像,您应该使用其他替代方法,尝试将图像存储在sqlite db中,然后重试或尝试使用firebase或parse云服务。除了sqlite db或firebase或parse之外,您是否还提到了其他代码使用sqlite db存储图像。实际上,每当我在Image1上单击share时,我的第一个图像都会被正确地共享。但当我在Image2上单击share时,我的第一张图像被抓取。Image3、4、5也是如此。。。我的应用程序非常基本..需要帮助吗??