我在Android中新 ..我想创建一个相机Android应用程序..
这些是以下代码...... 当我在我的手机[galaxy note n7000]中启动我的应用程序时它工作正常但是在捕获图像后出现问题..当我点击保存按钮时出现错误拨号并且我的应用程序停止..我认为 onActivityResult()方法..请帮忙..
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Take photo" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="49dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
MainActivity.java
package com.example.cameratest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn= (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
} });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bm= (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(bm);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameratest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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.example.cameratest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
嗨欢迎使用Android我在开始时也完成了这项任务。 这是camera.java的代码
`package com.example.android;
import java.io.IOException; import java.io.InputStream;
import android.app.Activity; import android.app.WallpaperManager; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView;
public class Camera extends Activity实现OnClickListener {
ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bm;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initialize();
InputStream is = getResources().openRawResource(
R.drawable.splash_background);
bm = BitmapFactory.decodeStream(is);
}
public void initialize() {
ib = (ImageButton) findViewById(R.id.ibTakePic);
b = (Button) findViewById(R.id.bSetWall);
iv = (ImageView) findViewById(R.id.ivReturnedPic);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bSetWall:
try {
WallpaperManager.getInstance(getApplicationContext())
.setBitmap(bm);
} 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();
bm = (Bitmap) extras.get("data");
iv.setImageBitmap(bm);
}
}
}
这是活动的xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/bSetWall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Wall" />
<ImageView
android:id="@+id/ivReturnedPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/ibTakePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/splash_background" />
答案 1 :(得分:0)
试试这个
像这样启动相机,在onCreate方法中使用此代码。
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
String filePath = Environment.getExternalStorageDirectory() + "";
filePath = filePath + File.separator + "temp_img.jpg";
File f = new File(filePath);
try {
if (!f.exists()) {
f.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
Uri uri = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 0);
onActivityResult方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
Uri currImageURI = null;
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
currImageURI = data.getData();
}
if (currImageURI == null) {
String filePath = Environment.getExternalStorageDirectory()
+ "";
filePath = filePath + File.separator + "temp_img.jpg";
File f = new File(filePath);
currImageURI = Uri.fromFile(f);
}
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr,
currImageURI);
iv.setImageBitmap(bitmap);
} catch (Exception e) {
Log.e("Camera", e.toString());
}
}
}
并确保在onCreate中初始化iv。
iv=(ImageView) findViewById(R.id.imageView1);
In Last添加在sdcard上写文件的权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:0)
一旦你显示你的logcat输出就可以找到确切的错误来源但似乎问题是你没有在任何地方初始化ImageView iv,但是你在onActivityResult方法中为它设置了位图。首先初始化imageView;可能在onCreate方法中。例如,在你的onCreate方法中执行此操作:
ImageView iv = (ImageView)findViewById(R.id.some_id);