Tuesday, July 7, 2015

Make Intene Action_View receiver

This is the way to make my app would be exposed to other app when clicking some file.

reference by developer site
http://developer.android.com/training/sharing/receive.html


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.example.viewer;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

import com.example.com.example.viewer.R;

public class MainActivity extends Activity{

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  
  File file = new File(Environment.getExternalStorageDirectory() + "/download/shared.txt");
    Toast.makeText(getApplication(), "lenth " + file.length(), 0).show();

      Toast.makeText(getApplication(), "is deleted? " + file.delete(), 0).show();
    
     // Get intent, action and MIME type
     Intent intent = getIntent();
     String action = intent.getAction();
     String type = intent.getType();
     Toast.makeText(getApplication(), "action" + action +" : type " + type, 1).show();
     if (Intent.ACTION_VIEW.equals(action) && type != null) {
         if ("text/plain".equals(type)) {
             handleSendText(intent); // Handle text being sent
         } else if (type.startsWith("image/")) {
             handleSendImage(intent); // Handle single image being sent
         }
     } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
         if (type.startsWith("image/")) {
             handleSendMultipleImages(intent); // Handle multiple images being sent
         }
     } else {
         // Handle other intents, such as being started from the home screen
     }
 }

 void handleSendText(Intent intent) {
     String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
     
     Uri uri= intent.getData();
     Toast.makeText(getApplication(), "pa " + uri.getPath(), 1).show();
     
     
     File file = new File(uri.getPath());
     
     Toast.makeText(getApplication(), "lenth " + file.length(), 0).show();

     
     Toast.makeText(getApplication(), "is deleted? " + file.delete(), 0).show();
     
     Toast.makeText(getApplication(), "lenth " + new File(uri.getPath()).length(), 0).show();
     
     
     
     Toast.makeText(getApplication(), "ajdidjdi" + sharedText, 1).show();
     Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
     if (imageUri != null) {
      String st = imageUri.getPath();
      //
      Toast.makeText(getApplication(), "" + st, 1).show();
     }
     
     if (sharedText != null) {
      // Update UI to reflect text being shared
      
      Toast.makeText(getApplication(), "" + sharedText, 1).show();
     }
 }

 void handleSendImage(Intent intent) {
     Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
     if (imageUri != null) {
         // Update UI to reflect image being shared
     }
 }

 void handleSendMultipleImages(Intent intent) {
     ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
     if (imageUris != null) {
         // Update UI to reflect multiple images being shared
     }
 }
}



ManifestFile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 <activity
            android:name="com.example.viewer.MainActivity"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>  
                <category android:name="android.intent.category.DEFAULT"/>  
                <category android:name="android.intent.category.BROWSABLE"/>  
            </intent-filter>         
            <intent-filter>
          <action android:name="android.intent.action.SEND" />
          <category android:name="android.intent.category.DEFAULT" />
          <data android:mimeType="image/*" />
      </intent-filter>
      <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <data android:mimeType="text/plain" />
      </intent-filter>
      <intent-filter>
          <action android:name="android.intent.action.SEND_MULTIPLE" />
          <category android:name="android.intent.category.DEFAULT" />
          <data android:mimeType="image/*" />
      </intent-filter>   
        </activity>

No comments:

Post a Comment