gpt4 book ai didi

android - 将拍摄的图像设置为墙纸的相机应用程序未获得所需的结果

转载 作者:行者123 更新时间:2023-11-29 01:45:20 25 4
gpt4 key购买 nike

这是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>

这是 list 文件

    <?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>

问题是,当我在手机上运行该应用程序时, 它会打开相机并拍照,但不会在应用程序的 ImageView 中显示拍摄的图像。 从相机返回后,拍摄的图像丢失了。 所以当我按下设置墙纸按钮时,它只是将默认图标设置为墙纸

最佳答案

在您的 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);
}
}

希望这对您有所帮助!

关于android - 将拍摄的图像设置为墙纸的相机应用程序未获得所需的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21845920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com