2016-07-03 85 views
0

在android中我无法以jpeg和png格式共享图片。请帮忙纠正我的代码在Android中分享图片的错误

每当我的共享代码它给
这是我的代码例外“不支持的文件格式”:

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); 
+1

该代码指向一个目录 – Chisko

+0

除此之外,相对较少的应用程序处理'android.resource'方案好。而且,并非每个应用都会处理*'EXTRA_TEXT' *和*'EXTRA_STREAM'。 – CommonsWare

+0

请更正我的代码 – Parekh

回答

0

Store中可绘制的图像到你的内部存储,并选择图片。这种方式可以帮助你。

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)); 
+0

谢谢库马尔。现在它的工作:) – Parekh

+0

兄弟如果我有大约50个图像,那么我将如何通过** R.drawable.shareforma ** ???需要帮助 – Parekh

+0

对于单个图像,此方法将适用。对于多个图像,您应该使用其他替代方法,尝试将图像存储在sqlite数据库中,然后尝试使用firebase或解析云服务。 –

相关问题