Last active
April 7, 2023 12:03
-
-
Save darkwave/7d5714a6323b4eee4df2 to your computer and use it in GitHub Desktop.
How to receive Android Intent data from another App using Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
For more information visit Android developer portal http://developer.android.com/training/sharing/receive.html | |
*/ | |
import android.graphics.BitmapFactory; | |
import android.graphics.Bitmap; | |
import java.io.InputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.net.Uri; | |
final int UNKNOWN_TYPE = -1; | |
final int IMAGE_TYPE = 0; | |
final int TEXT_TYPE = 1; | |
int contentType = UNKNOWN_TYPE; | |
Intent intent = null; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Get the intent that started this activity | |
intent = this.getIntent(); | |
//Uri data = intent.getData(); | |
if (intent == null) | |
return; | |
// Figure out what to do based on the intent type | |
if (intent.getType().indexOf("image/") != -1) { | |
// Handle intents with image data ... | |
contentType = IMAGE_TYPE; | |
} else if (intent.getType().equals("text/plain")) { | |
// Handle intents with text ... | |
contentType = TEXT_TYPE; | |
} | |
} | |
PImage receivedImage; | |
String receivedText = ""; | |
void setup() { | |
orientation(LANDSCAPE); | |
if (intent != null) { | |
switch (contentType) { | |
case IMAGE_TYPE: | |
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); | |
InputStream stream = null; | |
try { | |
stream = this.getContentResolver().openInputStream(imageUri); | |
} | |
catch (FileNotFoundException fileEx) { | |
} | |
if (stream != null) { | |
Bitmap bitmap = null; | |
try { | |
bitmap = BitmapFactory.decodeStream(stream); | |
} | |
finally { | |
try { | |
stream.close(); | |
stream = null; | |
} | |
catch (IOException e) { | |
} | |
} | |
receivedImage = new PImage(bitmap); | |
receivedImage.parent = this; | |
imageMode(CENTER); | |
} | |
break; | |
case TEXT_TYPE: | |
receivedText = intent.getStringExtra(Intent.EXTRA_TEXT); | |
textAlign(CENTER, CENTER); | |
break; | |
} | |
} | |
} | |
void draw() { | |
if (contentType == IMAGE_TYPE) | |
image(receivedImage, width / 2, height / 2, width, height); | |
else if (contentType == TEXT_TYPE) | |
text(receivedText, width / 2, height / 2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package=""><uses-sdk android:minSdkVersion="10"/><application android:debuggable="true" android:icon="@drawable/icon" android:label=""><activity android:name=""><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/><action android:name="android.intent.action.SEND"/><category android:name="android.intent.category.DEFAULT"/><data android:mimeType="text/plain"/><data android:mimeType="image/*"/></intent-filter></activity></application></manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment