0
我确定在某处有答案,但我似乎无法找到任何东西。 我试图做到这一点,当用户点击应用程序菜单上的项目时,它将开始意图到手机的画廊选择他们选择的图像来更改应用程序的背景,而不是预先设置的背景图像。有没有办法做到这一点。如何从手机图库中设置应用程序背景
这是我有什么代码,但是当我在手机上运行它时,应用程序崩溃。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
Intent sendIntent = null;
if (id == R.id.action_settings) {
sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Try Out this new app...");
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(sendIntent, 0);
boolean isIntentSafe = activities.size() > 0;
String title = getResources().getString(R.string.chooser_title);
Intent chooser = Intent.createChooser(sendIntent, title);
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
//noinspection SimplifiableIfStatement
if (id == R.id.change_background){
Intent pictureIntent = new Intent();
pictureIntent.setType("image/*");
pictureIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(pictureIntent,
“Select Picture”),0);
}
return true;
}}
谢谢!我没有onActivityResult,所以当我尝试复制你的代码时,我得到了一堆错误? –
@DeclanMorgan cretae该方法,并不只是复制code..try调试它,并根据您的需求进行更改 –