I am working on an app in which I want to open any photo in my app from external file explorer. Like when I click on photo, choose applications window appears and I select my app and image is shown in my app. But here, no image appears.
I added code in Manifest.xml
:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.image.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" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
Layout file activity_main.xml
<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>
And in my MainActivity.java
:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView picView = (ImageView) findViewById(R.id.picture);
TextView txtView = (TextView) findViewById(R.id.txt);
Intent receivedIntent = getIntent();
String receivedAction = receivedIntent.getAction();
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/")) {
picView.setVisibility(View.GONE);
String receivedText = receivedIntent
.getStringExtra(Intent.EXTRA_TEXT);
if (receivedText != null) {
txtView.setText(receivedText);
}
} else if (receivedType.startsWith("image/")) {
txtView.setVisibility(View.GONE);
Uri receivedUri = (Uri) receivedIntent
.getParcelableExtra(Intent.EXTRA_STREAM);
if (receivedUri != null) {
picView.setImageURI(receivedUri);// just for demonstration
}
}
} else if (receivedAction.equals(Intent.ACTION_VIEW)) {
if (receivedType.startsWith("text/")) {
Toast.makeText(this, "TextRecived", Toast.LENGTH_SHORT).show();
Uri uri2 = receivedIntent.getData();
String uri = uri2.getEncodedPath() + " complete: "
+ uri2.toString();
txtView.setText(uri);
} else if (receivedType.startsWith("image/")) {
txtView.setVisibility(View.GONE);
Uri receivedUri = (Uri) receivedIntent
.getParcelableExtra(Intent.EXTRA_STREAM);
Toast.makeText(this, receivedUri.toString(), Toast.LENGTH_SHORT).show();
if (receivedUri != null) {
picView.setImageURI(receivedUri);// just for demonstration
}
}
} else if (receivedAction.equals(Intent.ACTION_MAIN)) {
txtView.setText("-----------------------------");
}
}
}
Here are my screenshots:
The problem is that you are trying to get Uri
by wrong way. Change all occurances of
Uri receivedUri = (Uri) receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
to
Uri receivedUri = receivedIntent.getData();
this will get you the right Uri
of the file. Also do note that setImageURI
does
Bitmap reading and decoding on the UI thread
Refer this link for more details.