2016-07-03 89 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或解析雲服務。 –

相關問題