如何使用图库中的照片设置布局的背景?

时间:2013-11-20 13:24:36

标签: android background bitmap

我一直在尝试使用图库中的照片设置主要活动的背景。我添加了一个按钮并设置了它的监听器。在这个监听器中,我按如下方式定义了结果的意图:

Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"),
                0);

我用来接收结果并设置主要相对布局的背景的方法是:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 0:
        data.getDataString();
        if (resultCode == RESULT_OK) {
            try {

                Bitmap bitmap = MediaStore.Images.Media.getBitmap(
                        getContentResolver(), data.getData());
                RelativeLayout bg = (RelativeLayout) findViewById(R.id.mainRL);
                Drawable drawable = new BitmapDrawable(getResources(),
                        bitmap);
                bg.setBackgroundDrawable(drawable);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }
}

按下按钮后,图库就会打开,我可以选择照片,但之后会崩溃。崩溃发生时的logCat是:

11-20 21:32:53.381: D/dalvikvm(16734): GC_FOR_ALLOC freed 77K, 8% free 9300K/10051K, paused 20ms, total 20ms
11-20 21:32:53.391: I/dalvikvm-heap(16734): Grow heap (frag case) to 11.075MB for 1521016-byte allocation
11-20 21:32:53.431: D/dalvikvm(16734): GC_CONCURRENT freed <1K, 7% free 10785K/11591K, paused 14ms+2ms, total 37ms
11-20 21:32:53.721: I/webclipboard(16734): clipservice: android.sec.clipboard.ClipboardExManager@427823b0
11-20 21:32:53.791: I/webclipboard(16734): clipservice: android.sec.clipboard.ClipboardExManager@427823b0
11-20 21:32:53.891: D/dalvikvm(16734): GC_CONCURRENT freed 270K, 8% free 10993K/11847K, paused 13ms+12ms, total 40ms
11-20 21:32:54.012: I/webclipboard(16734): clipservice: android.sec.clipboard.ClipboardExManager@427823b0
11-20 21:32:54.282: D/dalvikvm(16734): DexOpt: --- BEGIN 'ads550927314.jar' (bootstrap=0) ---
11-20 21:32:54.412: D/dalvikvm(16734): GC_CONCURRENT freed 207K, 7% free 11673K/12487K, paused 16ms+16ms, total 87ms
11-20 21:32:54.422: V/webkit(16734): BrowserFrame constructor: this=Handler (android.webkit.BrowserFrame) {4277cc40}
11-20 21:32:54.452: D/ProgressBar(16734): setProgressDrawable mProgressDrawable = null, d = android.graphics.drawable.LayerDrawable@427997a8needUpdate = false
11-20 21:32:54.462: D/ProgressBar(16734): setProgress = 0
11-20 21:32:54.462: D/ProgressBar(16734): setProgress = 0, fromUser = false
11-20 21:32:54.462: D/ProgressBar(16734): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 17
11-20 21:32:54.472: W/ResourceType(16734): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
11-20 21:33:14.943: W/dalvikvm(16967): threadid=1: thread exiting with uncaught exception (group=0x41c612a0)
11-20 21:33:14.993: E/AndroidRuntime(16967): FATAL EXCEPTION: main
11-20 21:33:14.993: E/AndroidRuntime(16967): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.robotaaly.unitbulletconverter/com.robotaaly.unitbulletconverter.CalculationActivity}: java.lang.NullPointerException
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.os.Looper.loop(Looper.java:137)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.ActivityThread.main(ActivityThread.java:4867)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at java.lang.reflect.Method.invoke(Method.java:511)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at dalvik.system.NativeStart.main(Native Method)
11-20 21:33:14.993: E/AndroidRuntime(16967): Caused by: java.lang.NullPointerException
11-20 21:33:14.993: E/AndroidRuntime(16967):    at com.robotaaly.unitbulletconverter.CalculationActivity.calculateFnc(CalculationActivity.java:827)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at com.robotaaly.unitbulletconverter.CalculationActivity.access$0(CalculationActivity.java:825)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at com.robotaaly.unitbulletconverter.CalculationActivity$2.onTextChanged(CalculationActivity.java:404)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.widget.TextView.sendOnTextChanged(TextView.java:7398)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.widget.TextView.setText(TextView.java:3730)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.widget.TextView.setText(TextView.java:3585)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.widget.EditText.setText(EditText.java:95)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.widget.TextView.setText(TextView.java:3560)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.widget.TextView.onRestoreInstanceState(TextView.java:3460)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.view.View.dispatchRestoreInstanceState(View.java:12012)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.view.View.restoreHierarchyState(View.java:11990)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1826)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.Activity.onRestoreInstanceState(Activity.java:949)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.Activity.performRestoreInstanceState(Activity.java:921)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1145)
11-20 21:33:14.993: E/AndroidRuntime(16967):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
11-20 21:33:14.993: E/AndroidRuntime(16967):    ... 11 more
11-20 21:33:15.013: I/Ads(16967): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"preqs":0,"session_id":"14165905526806045037","seq_num":"1","slotname":"a1524d822019181","u_w":320,"msid":"com.robotaaly.unitbulletconverter","adtest":"on","js":"afma-sdk-a-v6.4.1","bas_off":0,"net":"null","app_name":"4.android.com.robotaaly.unitbulletconverter","hl":"en","gnt":8,"carrier":"50212","u_audio":1,"kw":[],"u_sd":1.5,"ms":"-JTEbDWqE5u7nUwF2aVwW9cvXVI5UmAeTnAwp_7nyxZY6Az-TVS6TEfG4RGORwQh4OqQNvA4oR5N_L9JJwubWzQazduuBeiGe7bnMOCDQDehxNgy8uuKeiZUmzaKyoyHopVay-XniTmGhjXJ0QQtpQb1Yc4y0rN87j8xGADQKXxX2aFnGkVp-iwhbRBPetLExiQ_U6m-eVeeogJ7Num_zQksZE7XdOds9ChR0idznXGE0sTZqaGy0O1L8oTsjrs7X4tczIDwM_s-nHjzXEQyUQzunKyzxBLsNNXfiPA_3_t-CoV1YDVB9kNltYsag0QpOFfIRhC0i8LfdcV-02mBTw","mv":"16.android","isu":"27B6214A0270F283668371709772C538","format":"320x50_mb","oar":0,"ad_pos":{"height":0,"visible":0,"y":0,"x":0,"width":0},"u_h":533,"pt":1,"bas_on":0,"ptime":0});</script></head><body></body></html>

这段代码有什么不对的地方? 谢谢

2 个答案:

答案 0 :(得分:1)

您的第一个问题是由于fromUnits返回的null值。当您按下后第二次应用程序崩溃是因为即使结果不正确,您也试图获取数据字符串。即在if (resultCode == RESULT_OK)之外。完全删除该语句,因为您没有使用它,或者如果需要,可以在if条件内移动它。

答案 1 :(得分:0)

启动选择图像的意图:

Intent galleryIntent = new  Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 002);

在此处接收您的图片:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
if (requestCode == 002 && resultCode == Activity.RESULT_OK) {
        Uri selectedImage = data.getData();
        //Get the picked image uri
        String picturePath = getRealPathFromURI(selectedImage);
        Bitmap bmp= BitmapFactory.decodeFile(picturePath, null);
        bg.setBackgroundDrawable(bmp);
    }
    super.onActivityResult(requestCode, resultCode, data);
}