从默认相机捕获图片并将其设置为墙纸

时间:2014-04-09 12:43:04

标签: java android

我尝试使用教程中的代码从默认相机捕捉图片并将其设置为墙纸。我使用图像按钮从相机拍摄照片,使用普通按钮将照片设置为墙纸。不幸的是,相机按钮没有响应点击它。我该如何解决这个问题?

我的sdk版本是" adt-bundle-windows-x86_64-20140321",这是我的代码。

相机[1]的Java代码:

package com.office.razer;

import java.io.IOException;

import android.app.WallpaperManager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener
{    
ImageButton ib;
Button b;
ImageView iv;
Intent i;
Bitmap bmp;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        initialize();
    }

    private void initialize() {
        // TODO Auto-generated method stub
        iv=(ImageView) findViewById(R.id._iv1);
        b=(Button) findViewById(R.id._b1);
        ib=(ImageButton) findViewById(R.id._ib1);
        b.setOnClickListener(this);
        b.setOnClickListener(this);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        case R.id._b1:
            try {
                getApplicationContext().setWallpaper(bmp);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case R.id._ib1:

            i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i,0);
            break;
            }
        }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK)
        {
            Bundle extras=data.getExtras();
        bmp=(Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
            }
    }   
    }

和xml代码[2]:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/_iv1"
        android:layout_width="166dp"
        android:layout_height="166dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <ImageButton
        android:id="@+id/_ib1"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_gravity="center"
        android:src="@drawable/abc_cab_background_bottom_holo_dark" />

    <Button
        android:id="@+id/_b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button" />
"

</LinearLayout>

这里是菜单java代码[3]:

package com.office.razer;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity {
    String classes[]={"MainActivity","Togglesw","Email","Camera","Contacts","Messages","WhatsAPP"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classes));
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        String cheese=classes[position];
        super.onListItemClick(l, v, position, id);
        Class ourclass;
        try {
            ourclass = Class.forName("com.office.razer."+cheese);
            Intent ourIntent=new Intent(Menu.this,ourclass);
            startActivity(ourIntent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

的manifest.xml [4]:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.office.razer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.office.razer.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
          <activity
            android:name=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.office.razer.Menu" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
         <activity
            android:name=".Togglesw"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.office.razer.TOGGLESW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity> <activity
            android:name=".Email"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.office.razer.EMAIL" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
         <activity
            android:name=".Camera"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.office.razer.CAMERA" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

0 个答案:

没有答案