gpt4 book ai didi

java - 通过相机捕获显示图像时 ImageView 错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:41 30 4
gpt4 key购买 nike

我是 Android 新手,我正在尝试让我的手机摄像头拍照并将其显示在 ImageView 中。捕获图像后,它会被正确保存,但之后应用程序会在图像应显示在 ImageView 中时停止工作。任何帮助表示赞赏。我搜索了一些主题,但仍然没有效果。这是代码:

package myfirstapp.myapps.me.camera;

import ...

public class MainActivity extends ActionBarActivity {
ImageButton camBtn;
ImageView imageView;
ScrollView scrollView;

private File imageFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void OpenCam(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"image.jpg");
Uri tempURI=Uri.fromFile(imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0)//==0 the same where startActivityForResult(intent, 0) so we are in the same process
{


switch (resultCode){
case Activity.RESULT_OK:
if(imageFile.exists())
{
Toast.makeText(MainActivity.this, "Image was saved at "+imageFile.getAbsolutePath(), Toast.LENGTH_SHORT)
.show();
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageView);

myImage.setImageBitmap(myBitmap);
}
else
{
Toast.makeText(MainActivity.this, "Image wasn't saved", Toast.LENGTH_SHORT)
.show();
}
break;
case Activity.RESULT_CANCELED:
Toast.makeText(MainActivity.this, "Image capture was cancelled", Toast.LENGTH_SHORT)
.show();
break;
}
}
}
}

堆栈跟踪

Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {myfirstapp.myapps.me.camera/myfirstapp.myapps.me.camera.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3410) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)

最佳答案

请仔细阅读此代码:

    public static final int REQUEST_IMAGE_CAPTURE = 1;
public static final int RESULT_LOAD_IMAGE = 10;
private Bitmap myBitmap;
ImageView myImage ;
public void OpenCam(View view) {

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{

startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}

还有,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{


if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
myBitmap= (Bitmap) extras.get("data");

myImage = (ImageView) findViewById(R.id.imageView);

myImage.setImageBitmap(myBitmap);
}

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

myBitmap= BitmapFactory.decodeFile(picturePath);

// photoUri = picturePath;



myImage.setImageBitmap(myBitmap);

}

}

关于java - 通过相机捕获显示图像时 ImageView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30753544/

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