我正在尝试在adroid上使用CAMERA意图来拍照并在图片视图中显示它。下面的代码工作正常,直到我拍照。然后我无法用“V”按钮确认并返回我的活动。如果我点击“X”按钮android回到我的活动没有照片,但如果我点击“V”确认图片,没有任何反应,我仍然卡在相机屏幕。
我正在使用API 14并使用android 4.0.2在虚拟和物理设备上进行测试。
我的错误在哪里?
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class HandScryActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri uriSavedImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handscry);
// Makes the filename
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image_001.jpg");
uriSavedImage = Uri.fromFile(image);
}
// Handles onGame clicked buttons
public void btnHandClick(View v) {
Button clickedButton = (Button) v;
// according to clicked button
switch (clickedButton.getId()) {
case R.id.btnBackToGame:
this.finish();
break;
case R.id.btnTakePicture:
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView img = (ImageView) findViewById(R.id.imgHand);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
// Display image
if (resultCode == RESULT_OK) {
img.setImageURI(uriSavedImage);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
}
答案 0 :(得分:1)
在这里你可以找到解决方案,万一其他人会在我的错误中绊倒。我从一个网站上得到了这个样本,但这个部分似乎卡住了android,并且在成功的情况下无法获得“OnActivityResult”:
// Makes the filename
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image_001.jpg");
uriSavedImage = Uri.fromFile(image);
应替换为
File image = new File(Environment.getExternalStorageDirectory(), "test.jpg");
uriSavedImage = Uri.fromFile(image);
答案 1 :(得分:0)
在我的情况下,问题是由于在启动android.provider.MediaStore.EXTRA_OUTPUT
活动时在版本4.4.4上使用FileProvider
生成的ACTION_IMAGE_CAPTURE
引起的。
当前活动是这样开始的:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoURI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? FileProvider.getUriForFile(context, context.getPackageName(),photoFile)
: Uri.fromFile(photoFile);
// write the captured image to a file
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoURI);
// start it from the activity and not from the dialog so the request code will be the
// one expected and not the one from the dialog
activity.startActivityForResult(intent, CAMERA_REQUEST_CODE);
希望它有所帮助。
答案 2 :(得分:0)
我正面临类似的问题。我能够打开相机意图然后捕获图像,但是无法按一下One Plus 6T上的“确定”(勾号)按钮。调试没有帮助。 然后我发现了这个问题与内存优化有关。我杀死了所有其他后台应用程序,然后再试一次,就可以了。
有关更多信息,refer this link。 选项2在此链接中对我有用。
从链接中复制粘贴答案:
"When returning from the camera intent sometimes the app was being destroyed by the operating system (I'm assuming for memory
reasons). This resulted in sporadic behavior and my state object
would have several null fields upon returning, making my app crash.
Fix: Write state object to file before sending picture, load in my parent activity class which handles onCreate then goes to the subclassed activity."