Setelah bisa membuat Aplikasi Android Share Teks Status ke Aplikasi Lain, Misal Facebook App atau Twitter App. Tentu saja kita ingin membuat aplikasi pasangannya, yaitu sebaliknya Aplikasi yang Dapat Menerima Sharing Gambar dari Aplikasi Lain, Receive Sharing Intent dari Aplikasi Seperti Image Gallery.
Kita mulai saja, buat Project baru. Ketikkan source code ini pada MainActivity.java :
package com.amijaya.receivesharesendintentimage;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
// http://cariprogram.blogspot.com
// nuramijaya@gmail.com
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the image view
ImageView picView = (ImageView)findViewById(R.id.picture);
//get the text view
TextView txtView = (TextView)findViewById(R.id.txt);
//get the received intent
Intent receivedIntent = getIntent();
//get the action
String receivedAction = receivedIntent.getAction();
//find out what we are dealing with
String receivedType = receivedIntent.getType();
//make sure it's an action and type we can handle
if(receivedAction.equals(Intent.ACTION_SEND)){
if(receivedType.startsWith("text/")){
//hide the other ui item
picView.setVisibility(View.GONE);
//get the received text
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);
//check we have a string
if (receivedText != null) {
//set the text
txtView.setText(receivedText);
}
}
else if(receivedType.startsWith("image/")){
//hide the other ui item
txtView.setVisibility(View.GONE);
//get the uri of the received image
Uri receivedUri = (Uri)receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
//check we have a uri
if (receivedUri != null) {
//set the picture
//RESAMPLE YOUR IMAGE DATA BEFORE DISPLAYING
//see http://developer.android.com/training/displaying-bitmaps/index.html
picView.setImageURI(receivedUri);//just for demonstration
}
}
}
else if(receivedAction.equals(Intent.ACTION_MAIN)){
//app has been launched directly, not from share list
txtView.setText("Tidak ada yang disharing!");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".DataReceiverActivity" >
<ImageView
android:id="@+id/picture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Receive Picture Sharing"
/>
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Nah ini bagian yang terpenting, yang biasanya kita jarang mengedit konfigurasi di Android Manifest, sekarang ini harus diperhatikan baik-baik karena konfigurasi inilah sehingga Aplikasi yang kita buat terdaftar dan dianggap mampu menerima Sharing Gambar oleh aplikasi lain.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amijaya.receivesharesendintentimage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.amijaya.receivesharesendintentimage.MainActivity"
android:label="@string/app_name" >
<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.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
</manifest>
Hasilnya, coba kita buka Image Gallery dan coba buka salah satu gambar, klik menu Share. Nama Aplikasi kita akan muncul di Pilihan. Klik.
Hasilnya Aplikasi kita mampu menangkap Gambar yang dishare tersebut :
Project seutuhnya dapat didownload disini. Jika mengalami hambatan dalam silakan baca cara mengunduhnya
Semoga menarik :)
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :