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調試它,並根據您的需求進行更改 –