2016-11-14 45 views
2

我的目標是從的ImageView共享圖像內RecyclerView到apps.The圖像從使用畢加索LIB 火力地堡存儲加載的其他。如何分享RecyclerView中的圖像?

我在每個項目下添加了一個「共享」按鈕,但無法繼續進一步操作。

這是我的MainActivity。

public class MainActivity extends AppCompatActivity { 

private RecyclerView mBlogList; 

private DatabaseReference mDatabase; 

private LinearLayoutManager mLayoutManager; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog"); 
    mDatabase.keepSynced(true); 
    mBlogList = (RecyclerView) findViewById(R.id.blog_List); 
    mBlogList.setHasFixedSize(true); 
    mBlogList.setLayoutManager(new LinearLayoutManager(this)); 
    mLayoutManager = new LinearLayoutManager(MainActivity.this); 
    mLayoutManager.setReverseLayout(true); // THIS ALSO SETS  setStackFromBottom to true 
    mBlogList.setLayoutManager(mLayoutManager); 
    mLayoutManager.setStackFromEnd(true); 




} 

@Override 
protected void onStart() { 
    super.onStart(); 

FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new   FirebaseRecyclerAdapter<Blog, BlogViewHolder>(

      Blog.class, 
      R.layout.blog_row, 
      BlogViewHolder.class, 
      mDatabase 
    ) { 

     @Override 

     protected void populateViewHolder(BlogViewHolder viewHolder, Blog model,int position) { 
      viewHolder.setTitle(model.getTitle()); 

      viewHolder.setImage(getApplicationContext(), model.getImage()); 


     } 
    }; 

    mBlogList.setAdapter(firebaseRecyclerAdapter); 



} 

public static class BlogViewHolder extends RecyclerView.ViewHolder { 
    View mView; 


    public BlogViewHolder(View itemView) { 
     super(itemView); 
     mView = itemView; 
    } 


    public void setTitle(String title) { 
     TextView post_title = (TextView) mView.findViewById(R.id.post_title); 
     post_title.setText(title); 
    } 

    public void setImage(final Context ctx, final String image) { 
     final ImageView post_image = (ImageView) mView.findViewById(R.id.post_image); 
     Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() { 
      @Override 
      public void onSuccess() { 

      } 

      @Override 
      public void onError() { 
       //Try again online if cache failed 
       Picasso.with(ctx) 
         .load(image) 
         .error(R.drawable.header) 
         .into(post_image, new Callback() { 
          @Override 
          public void onSuccess() { 

          } 

          @Override 
          public void onError() { 
           Log.v("Picasso", "Could not fetch image"); 
          } 



         }); 



      } 
     }); 
    } 



} 
+0

你想分享圖像到其他應用程序的位置? –

+0

我很抱歉沒有具體。是我想分享圖像到其他應用程序,以便用戶可以在fb或whatsapp上共享圖像。 –

+0

FB不允許用戶共享其他應用程序的圖像,你可以共享圖像的鏈接,它會爲FB和Whats應用程序做你的工作 –

回答

1

您可以使用下面的代碼與可用的選項共享(推特,臉譜,Gmail中,WhatsApp的,等...):

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_TEXT, "image text"); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath)); 
shareIntent.setType("image/*"); 
startActivity(Intent.createChooser(shareIntent, "Share image via:")); 

我沒有嘗試,但基本上是這樣的。 Facebook可能不允許與其他應用程序共享。那麼在哪裏使用Facebook SDK(GraphApi)

編輯

參考官方文檔

https://developer.android.com/training/sharing/index.html

+0

我已經試過這個,但它沒有爲我工作,我想我做錯了什麼。 –

+0

你會得到什麼錯誤或警告?它必須工作。我建議你閱讀https://developer.android.com/training/sharing/index.html – uguboz

+0

先生你的代碼工作正常,但問題是與filepath.What將文件路徑?我已經創建了一個方法來解析uri但我不知道如何使用它共享意圖。 –

1

你需要轉換的ImageView爲位圖,然後先分享它。

// Can be triggered by a view event such as a button press 
public void onShareItem(View v) { 
    // Get access to bitmap image from view 
    ImageView ivImage = (ImageView) findViewById(R.id.post_image); 
    // Get access to the URI for the bitmap 
    Uri bmpUri = getLocalBitmapUri(ivImage); 
    if (bmpUri != null) { 
     // Construct a ShareIntent with link to image 
     Intent shareIntent = new Intent(); 
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
     shareIntent.setType("image/*"); 

     // Launch sharing dialog for image 
     startActivity(Intent.createChooser(shareIntent, "Share Image")); 

    } else { 

    } 
} 

// Returns the URI path to the Bitmap displayed in specified ImageView 
public Uri getLocalBitmapUri(ImageView imageView) { 
    // Extract Bitmap from ImageView drawable 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 
    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     return null; 
    } 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
} 

如何在viewholder中設置共享按鈕。

protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) { 
      viewHolder.setTitle(model.getTitle()); 

      viewHolder.setImage(getApplicationContext(), model.getImage()); 


      viewHolder.mShareButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        onShareItem(view); 



       } 
      }); 


     } 
    }; 

    mBlogList.setAdapter(firebaseRecyclerAdapter); 


} 

public static class BlogViewHolder extends RecyclerView.ViewHolder { 
    View mView; 

    Button mShareButton; 


    public BlogViewHolder(View itemView) { 
     super(itemView); 
     mView = itemView; 
     mShareButton = (Button) mView.findViewById(R.id.btn_share); 
    }