2017-09-04 64 views
-1

我正在處理一個Gallery類型的應用程序。我想添加一個分享選項,只要特定的圖像被點擊,但沒有這樣做。添加選項以從適配器共享圖像

我試圖創建intents,它沒有工作。我該怎麼辦?

這是我的適配器類別代碼:https://gist.github.com/mukundmadhav/e63fcf7e34414b9b88458400a6d55651

(也用於測試我已保存在可繪製文件夾8張圖片)

當我使用意圖它顯示了該錯誤消息: 錯誤:(51,17 )錯誤:無法找到符號方法startActivity(意圖)

我已經導入了android.content.Intent但錯誤仍然存​​在。

+0

我看到了你的代碼。它沒有'ShareActionProvider'的任何部分。閱讀關於android在這裏共享https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents –

+0

沒有錯誤。我想知道我該怎麼做。我不知道。 –

回答

0

試試這個適配器:

viewHolder.img.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

     //here write code to share image //get clicked image bitmap and then share 
    Bitmap bitmap = ((BitmapDrawable)viewHolder.img.getDrawable()).getBitmap(); 
    Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setType("image/png"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap , "Title", null); 
    // Uri uri = Uri.parse("android.resource://your package 
    //name/"+R.drawable.ic_launcher); 
    Uri uri = Uri.parse(path); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing"); 
    startActivity(Intent.createChooser(shareIntent, "Send your image")); 

        } 
       }); 
+0

我知道如何製作onclicklistener。我想知道我在這裏寫什麼://在這裏寫代碼共享圖像//獲取點擊圖像位圖,然後共享 –

+0

編寫的代碼由@jigar savaliya給出 –

+0

當我使用Intents時,它顯示此錯誤消息:錯誤:(51 ,17)錯誤:無法找到符號方法startActivity(意圖) –