1 /**
2  * Copyright (c) 2015, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.content;
18 
19 import android.app.Activity;
20 import android.content.ClipData;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.util.TypedValue;
25 import android.view.View;
26 import android.widget.Button;
27 import com.example.android.apis.R;
28 
29 /**
30  * Example of sharing content from a private content provider.
31  */
32 public class ShareContent extends Activity {
33     @Override
onCreate(Bundle savedInstanceState)34     protected void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36 
37         setContentView(R.layout.share_content);
38 
39         // Watch for button clicks.
40         ((Button)findViewById(R.id.share_image)).setOnClickListener(new View.OnClickListener() {
41             @Override public void onClick(View v) {
42                 Intent intent = new Intent(Intent.ACTION_SEND);
43                 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
44                 Uri.Builder b = new Uri.Builder();
45                 b.scheme("content");
46                 b.authority("com.example.android.apis.content.FileProvider");
47                 TypedValue tv = new TypedValue();
48                 getResources().getValue(R.drawable.jellies, tv, true);
49                 b.appendEncodedPath(Integer.toString(tv.assetCookie));
50                 b.appendEncodedPath(tv.string.toString());
51                 Uri uri = b.build();
52                 intent.setType("image/jpeg");
53                 intent.putExtra(Intent.EXTRA_STREAM, uri);
54                 intent.setClipData(ClipData.newUri(getContentResolver(), "image", uri));
55                 startActivity(Intent.createChooser(intent, "Select share target"));
56             }
57         });
58     }
59 }
60