Reputation: 595
I am creating an app to pick image from system and set it as wallpaper this is the code
ppublic class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button submit = (Button) findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select picture"), 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
WallpaperManager wall=WallpaperManager.getInstance(getApplicationContext());
Intent intent = new Intent(wall.getCropAndSetWallpaperIntent(data.getData()));
startActivity(intent);
}
}
I am getting runtime exception
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent {dat=file:///storage/emulated/0/DCIM/Camera/IMG_20160517_150558.jpg typ=image/jpeg }}
to activity
{com.prime.alpha.test/com.prime.alpha.test.MainActivity}: : java.lang.IllegalArgumentException: Image URI must be of the content scheme type
Upvotes: 0
Views: 192
Reputation: 3766
Activity may not found and you get that exception (ActivityNotFoundException
). Use try/catch:
public void onClick(View view){
WallpaperManager w = WallpaperManager.getInstance(getApplicationContext());
try {
Intent intent = new Intent(WallpaperManager.ACTION_CROP_AND_SET_WALLPAPER);
startActivity(intent);
} catch(ActivityNotFoundException e) {
e.printStackTrace();
}
}
Upvotes: 1
Reputation: 327
The error message is "No Activity found to handle Intent". The problem is there's no application installed, that can chose an image for you. If you have a gallery app insalled, they usually can chose an image.
This relates the way the Android OS handles implicit intents. Unlike explicit intent where you specify which class you address your intent to, you just say "do this for me", in your case "give me an image". The system then searches for applications that satisfies your criterias (getcontent and image). If there's no application capable of what you want, you get this error.
You can make sure that there's an application that can handle your request by resolving the intent first, and if there's no application, you don't start your intent, but for example show an error message.
if (intentntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Select picture"), 0);
} else {
Toast.makeText(this, "Can't find an application to select image", Toast.LENGTH_SHORT).show();
//you can use getApplicationContext() too as context, or getActivity() from a fragment
}
Upvotes: 0