我是初学者。不知道我搞砸了哪里。所以任何帮助都非常感谢。 第二个活动有一个按钮,意图打开第三个活动。第3个活动应该开始使用相机功能打开,并在显示之前等待拍摄图像。然而,第三个活动一开始就失败了。 所以这是代码。
Java代码(第二个活动)。
public class CameraView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_view);
// Show the Up button in the action bar.
}
public void onClickCam(View view) {
Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Java代码(第3个活动)。
public class ResultActivity extends Activity {
private static final int ACTION_TAKE_PHOTO_B = 1;
private static final String BITMAP_STORAGE_KEY = "viewbitmap";
private static final String IMAGEVIEW_VISIBILITY_STORAGE_KEY = "imageviewvisibility";
private ImageView mImageView;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private static final String JPEG_FILE_PREFIX = "IMG_";
private static final String JPEG_FILE_SUFFIX = ".jpg";
private AlbumStorageDirFactory mAlbumStorageDirFactory = null;
/* Photo album for this application */
private String getAlbumName() {
return getString(R.string.album_name);
}
private File getAlbumDir() {
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
storageDir = mAlbumStorageDirFactory.getAlbumStorageDir(getAlbumName());
if (storageDir != null) {
if (! storageDir.mkdirs()) {
if (! storageDir.exists()){
Log.d("CameraSample", "failed to create directory");
return null;
}
}
}
} else {
Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
}
return storageDir;
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File albumF = getAlbumDir();
File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, albumF);
return imageF;
}
private File setUpPhotoFile() throws IOException {
File f = createImageFile();
mCurrentPhotoPath = f.getAbsolutePath();
return f;
}
private void setPic() {
/* There isn't enough memory to open up more than a couple camera photos */
/* So pre-scale the target bitmap into which the file is decoded */
/* Get the size of the ImageView */
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scaleFactor = 1;
if ((targetW > 0) || (targetH > 0)) {
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
}
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
/* Associate the Bitmap to the ImageView */
mImageView.setImageBitmap(bitmap);
mImageView.setVisibility(View.VISIBLE);
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
switch(actionCode) {
case ACTION_TAKE_PHOTO_B:
File f = null;
try {
f = setUpPhotoFile();
mCurrentPhotoPath = f.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
} catch (IOException e) {
e.printStackTrace();
f = null;
mCurrentPhotoPath = null;
}
break;
default:
break;
} // switch
startActivityForResult(takePictureIntent, actionCode);
}
private void handleBigCameraPhoto() {
if (mCurrentPhotoPath != null) {
setPic();
galleryAddPic();
mCurrentPhotoPath = null;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
mImageView = (ImageView) findViewById(R.id.imageView1);
mImageBitmap = null;
Intent intent = new Intent(this, ResultActivity.class);
dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
startActivity(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
} else {
mAlbumStorageDirFactory = new BaseAlbumDirFactory();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTION_TAKE_PHOTO_B: {
if (resultCode == RESULT_OK) {
handleBigCameraPhoto();
}
break;
} // ACTION_TAKE_PHOTO_B
} // switch
}
// Some lifecycle callbacks so that the image can survive orientation change
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelable(BITMAP_STORAGE_KEY, mImageBitmap);
outState.putBoolean(IMAGEVIEW_VISIBILITY_STORAGE_KEY, (mImageBitmap != null) );
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mImageBitmap = savedInstanceState.getParcelable(BITMAP_STORAGE_KEY);
mImageView.setImageBitmap(mImageBitmap);
mImageView.setVisibility(
savedInstanceState.getBoolean(IMAGEVIEW_VISIBILITY_STORAGE_KEY) ?
ImageView.VISIBLE : ImageView.INVISIBLE
);
}
/**
* Indicates whether the specified action can be used as an intent. This
* method queries the package manager for installed packages that can
* respond to an intent with the specified action. If no suitable package is
* found, this method returns false.
* http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
*
* @param context The application's environment.
* @param action The Intent action to check for availability.
*
* @return True if an Intent with the specified action can be sent and
* responded to, false otherwise.
*/
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
}
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=".ResultActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
logcat的
08-26 01:45:55.845: E/AndroidRuntime(3471): FATAL EXCEPTION: main
08-26 01:45:55.845: E/AndroidRuntime(3471): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camoid/com.example.camoid.ResultActivity}: java.lang.NullPointerException
08-26 01:45:55.845: E/AndroidRuntime(3471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-26 01:45:55.845: E/AndroidRuntime(3471): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-26 01:45:55.845: E/AndroidRuntime(3471): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-26 01:45:55.845: E/AndroidRuntime(3471): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-26 01:45:55.845: E/AndroidRuntime(3471): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 01:45:55.845: E/AndroidRuntime(3471): at android.os.Looper.loop(Looper.java:137)
08-26 01:45:55.845: E/AndroidRuntime(3471): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-26 01:45:55.845: E/AndroidRuntime(3471): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 01:45:55.845: E/AndroidRuntime(3471): at java.lang.reflect.Method.invoke(Method.java:525)
08-26 01:45:55.845: E/AndroidRuntime(3471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-26 01:45:55.845: E/AndroidRuntime(3471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-26 01:45:55.845: E/AndroidRuntime(3471): at dalvik.system.NativeStart.main(Native Method)
08-26 01:45:55.845: E/AndroidRuntime(3471): Caused by: java.lang.NullPointerException
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.getAlbumDir(ResultActivity.java:47)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.createImageFile(ResultActivity.java:69)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.setUpPhotoFile(ResultActivity.java:76)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.dispatchTakePictureIntent(ResultActivity.java:135)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.onCreate(ResultActivity.java:171)
08-26 02:33:57.415: E/AndroidRuntime(3730): at android.app.Activity.performCreate(Activity.java:5133)
08-26 02:33:57.415: E/AndroidRuntime(3730): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-26 02:33:57.415: E/AndroidRuntime(3730): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
答案 0 :(得分:1)
完整的解决方案
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
private String imgPath;
btnGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
}
});
btnCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == PICK_IMAGE) {
selectedImagePath = getAbsolutePath(data.getData());
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else if (requestCode == CAPTURE_IMAGE) {
selectedImagePath = getImagePath();
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
答案 1 :(得分:1)
dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
startActivity(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
} else {
mAlbumStorageDirFactory = new BaseAlbumDirFactory();
}
此处dispatchTakePictureIntent()
间接调用使用getAlbumDir()
的函数mAlbumStorageDirFactory
,该函数尚未初始化,如logcat中所示:
08-26 01:45:55.845: E/AndroidRuntime(3471): Caused by: java.lang.NullPointerException
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.getAlbumDir(ResultActivity.java:47)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.createImageFile(ResultActivity.java:69)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.setUpPhotoFile(ResultActivity.java:76)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.dispatchTakePictureIntent(ResultActivity.java:135)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.onCreate(ResultActivity.java:171)
在致电mAlbumStorageDirFactory
之前设置dispatchTakePictureIntnet()
。
答案 2 :(得分:0)
我看到两种可能性。您要么a)忘记在清单中宣布您的第二项活动,要么b)启动第二项活动的意图不正确。
如果a)
您需要在<application>
代码
<activity
android:name=".ResultActivity"
android:label="@string/app_name" >
</activity>
如果b)
这是开展活动的一种方式:
startActivityForResult(new Intent(this, ResultActivity.class), 0);