Android 我想通过我的应用程序在主布局背景中设置保存在sd卡上的图像

Android 我想通过我的应用程序在主布局背景中设置保存在sd卡上的图像,android,android-layout,android-sdcard,Android,Android Layout,Android Sdcard,我正在创建一个应用程序,我想在主xml linearlayout中设置不同的背景图像。我在sd卡上存储了5个图像文件。现在我想选择一个pic并将其设置为我的主xml linearlayout背景。因此,它将替换以前的图像并将新图像显示为背景。首先为主xml分配一个idlinearlayout,例如,在以下情况下,它被命名为“容器” 问候 Ziteng Chen谢谢你的代码,它工作得很好。但是,如果我试图在闪屏上使用代码,它不会出现。你能告诉我需要做哪些更改吗?我想在应用程序启动时在启动屏幕上从S

我正在创建一个应用程序,我想在主xml linearlayout中设置不同的背景图像。我在sd卡上存储了5个图像文件。现在我想选择一个pic并将其设置为我的主xml linearlayout背景。因此,它将替换以前的图像并将新图像显示为背景。

首先为主xml分配一个idlinearlayout,例如,在以下情况下,它被命名为“容器”

问候


Ziteng Chen

谢谢你的代码,它工作得很好。但是,如果我试图在闪屏上使用代码,它不会出现。你能告诉我需要做哪些更改吗?我想在应用程序启动时在启动屏幕上从SD卡加载一个图像。代码帮助了我。。我能够从外部存储器上的图像设置布局的背景图像,甚至启动屏幕图像,这要感谢我的帮助:)
    <!-- main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/container">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
</LinearLayout>
package org.example.app;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String pathName = "/sdcard/gif001.gif";
        Resources res = getResources();
        Bitmap bitmap = BitmapFactory.decodeFile(pathName);
        BitmapDrawable bd = new BitmapDrawable(res, bitmap);
        View view = findViewById(R.id.container);
        view.setBackgroundDrawable(bd);
    }
}