好的,这显然是一个重复的问题,但在所有其他问题上,我找不到有效的解决方案......
所以,我试图用相机拍照并在imageview中显示这张图片...现在,如果效率很高或任何其他事情都不重要,那么我就不会这么做。我没有处理这件事......
这是我的代码:
运行相机:
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePicture.resolveActivity(getPackageManager()) != null) {
File image = null;
try {
image = createImageFile();
} catch (IOException ex) {
finish();
}
if(image != null) {
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(takePicture, 1);
}
}
}
});
createImageFile
方法:
private File createImageFile() throws IOException {
String name = System.currentTimeMillis() / 1000 + "";
File dir = getExternalFilesDir(null);
File image = File.createTempFile(name, ".png", dir);
path = "file:" + image.getAbsolutePath();
return image;
}
和onActiviyResult
方法:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && requestCode == 1) {
String path = data.getExtras().getString(MediaStore.EXTRA_OUTPUT);
File f = new File(path);
Bitmap b = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(f.getAbsolutePath()), 50, 50, true);
iv.setImageBitmap(b);
} else {
}
}
重要的一点是,我的应用程序永远不会到达onActivityResult
方法,但在我拍摄图片后点击保存并单击保存以保存图片(此屏幕显示图像拍摄后,它不是我实施的但是它&# 39;它在Android上的方式)
这是我的logcat:
04-08 14:57:37.230 5976-5976/hr.fer.dummyproj E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {hr.fer.dummyproj/hr.fer.dummyproj.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
at android.app.ActivityThread.access$1200(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at hr.fer.dummyproj.MainActivity.onActivityResult(MainActivity.java:84)
at android.app.Activity.dispatchActivityResult(Activity.java:5563)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
at android.app.ActivityThread.access$1200(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
更新
所以,我发现了问题所在。如果它将图片保存在文件中,看起来相机活动不会在Intent中返回任何内容。因此,在onResultActivity
中,数据为空。
为了使它成功,我保存了Uri并在onResultActivity
中获取了它的图片
答案 0 :(得分:0)
这对我有用....
static final int REQUEST_IMAGE_CAPTURE = 1;
String mCurrentPhotoPath;
Button captureImageButton;
static ImageView imageCapturedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_image);
captureImageButton = (Button) findViewById(R.id.button);
OnClickListener captureImageListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent captureImageIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(captureImageIntent.resolveActivity(getPackageManager())!=null)
{
File photoFile=null;
try{
photoFile = createImageFile();
}catch(IOException e){};
if(photoFile != null)
{
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(captureImageIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
};
captureImageButton.setOnClickListener(captureImageListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
imageCapturedView = (ImageView)findViewById(R.id.ImageView);
if(requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap= (Bitmap) extras.get("data");
imageCapturedView.setImageBitmap(imageBitmap);
galleryAddPic();
setPic();
}
}
private File createImageFile() throws IOException
{
String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());
String ImageFile = "JPEG_" + TimeStamp + "_";
File StorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(ImageFile, ".jpg", StorageDir);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void galleryAddPic()
{
Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f= new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScan.setData(contentUri);
this.sendBroadcast(mediaScan);
}
private void setPic() throws ArithmeticException{
int scaleFactor;
Bitmap bitmap;
// Get the dimensions of the View
int targetW = imageCapturedView.getWidth();
int targetH = imageCapturedView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
//bmOptions.inSampleSize = 4;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
scaleFactor= Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
Toast.makeText(getApplicationContext(), mCurrentPhotoPath, Toast.LENGTH_LONG).show();
imageCapturedView.setImageBitmap(bitmap);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.take_image, menu);
return true;
}