这是camera.java
文件
package com.newpackage.myapp;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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;
int cameraResults;
final static int cameraData = 0;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initialize();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void initialize(){
iv = (ImageView) findViewById (R.id.ivReturnPic);
ib = (ImageButton) findViewById(R.id.ibTakePic);
b = (Button) findViewById(R.id.bSetWall);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
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);
}
}
}
这是photo.xml
<?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/ivReturnPic"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/ibTakePic"
android:layout_gravity="center"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/bSetWall"
android:layout_gravity="center"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:text="Set Wallpaper" />
</LinearLayout>
这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.newpackage.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.newpackage.myapp.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="com.newpackage.myapp.Startingpoint"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.newpackage.myapp.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.newpackage.myapp.Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.newpackage.myapp.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.newpackage.myapp.TextPlay"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.newpackage.myapp.Email"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.newpackage.myapp.Camera"
android:label="Camera Application"
android:screenOrientation="portrait"
>
</activity>
</application>
</manifest>
问题在于,当我在手机上运行应用时, 它会打开相机并拍照,但它不会显示应用程序中图像视图中拍摄的图像。 从相机返回后,拍摄的图像丢失。 因此,当我按下设置壁纸按钮时,它只将默认图标设置为壁纸
答案 0 :(得分:1)
在你的onActivityResult
中,你放了:
if (resultCode == RESULT_OK){
...
}
你应该把:
if (resultCode == RESULT_OK && requestCode==cameraData){
...
}
这就是你知道结果来自Camera Intent ...
的方式编辑:
尝试使用此onActivityResult
代替您:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == cameraData && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(photo);
}
}
希望这有帮助!