我正在开发使用 ImageView 来显示照片的应用程序。但是拍摄照片后,它不会显示在 ImageView 中。此外,应用程序会在拍照后强制关闭。我用我的 Nexus 5 (Marshmallow)、Samsung Galaxy S4(Lollipop & CustomRom)、小米 Mi4、Asus Zenfone 1 和 Lenovo 平板电脑试了一下,它们都运行流畅。以下是我尝试使用三星 Galaxy S5 的一些场景:
- 拍照,等待几秒钟,然后单击“确定”,然后按回我的应用程序。(结果:有时这可行,有时图像不会显示在 ImageView 中)
- 拍照并立即单击“确定”。(结果:强制关闭。Logcat 说抽象类中的 TextView 为 null)
- 拍照,在函数 OnActivityResult 中调试时立即单击“确定”(结果:有效)
这是代码:
AndroidManifest.xml
<application
android:name=".coreclass.CoreApp"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/unik_fintech"
android:label="@string/appname"
android:largeHeap="true"
android:launchMode="singleInstance"
android:noHistory="true"
android:theme="@style/Theme.MyTheme"
tools:replace="android:label">
<activity
android:name=".activities.MainPage"
android:configChanges="screenSize|keyboardHidden|orientation"
android:exported="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.KantinActivity"
android:configChanges="screenSize|keyboardHidden|orientation"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
/>
KantinActivity.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Timber.d("isi request code:" + String.valueOf(requestCode));
Timber.d("isi result Code:"+ String.valueOf(resultCode));
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case RESULT_GALERY:
if(resultCode == RESULT_OK){
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.kantinActivityContent);
if(currentFrag instanceof StockBarangAction){
Bitmap photo = null;
Uri _urinya = data.getData();
if(data.getData() == null) {
photo = (Bitmap)data.getExtras().get("data");
} else {
try {
photo = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
GeneralizeImage mGI = new GeneralizeImage(this,photo,_urinya);
StockBarangAction mFrag = (StockBarangAction) currentFrag;
mFrag.setFileImageCatalog(false, mGI.Convert(), _urinya.toString());
}
}
break;
case RESULT_CAMERA:
if(resultCode == RESULT_OK && mCapturedImageURI!=null){
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.kantinActivityContent);
if(currentFrag instanceof StockBarangAction){
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(mCapturedImageURI, projection, null, null, null);
String filePath;
if (cursor != null) {
cursor.moveToFirst();
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
filePath = cursor.getString(column_index_data);
}
else
filePath = data.getData().getPath();
GeneralizeImage mGI = new GeneralizeImage(this,filePath);
StockBarangAction mFrag = (StockBarangAction) currentFrag;
mFrag.setFileImageCatalog(true, mGI.Convert(), filePath);
if (cursor != null) {
cursor.close();
}
}
}
else{
Toast.makeText(this, "Try Again", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mCapturedImageURI != null) {
outState.putString("cameraImageUri", String.valueOf(mCapturedImageURI));
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey("cameraImageUri")) {
mCapturedImageURI = Uri.parse(savedInstanceState.getString("cameraImageUri"));
}
}
public void setmCapturedImageURI(Uri _uri) {
mCapturedImageURI = _uri;
}
StockBarangAction.java
private void chooseCamera() {
if (reqPermissionClass.checkPermission(Manifest.permission.CAMERA, ReqPermissionClass.PERMISSIONS_REQ_CAMERA)) {
runCamera();
}
}
public void runCamera(){
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
KantinActivity parent = (KantinActivity) getActivity();
parent.setmCapturedImageURI(mCapturedImageURI);
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
getActivity().startActivityForResult(takePictureIntent, KantinActivity.RESULT_CAMERA);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (reqPermissionClass.checkOnPermissionResult(requestCode,grantResults,
ReqPermissionClass.PERMISSIONS_REQ_WRITEEXTERNALSTORAGE)||
reqPermissionClass.checkOnPermissionResult(requestCode,grantResults,
ReqPermissionClass.PERMISSIONS_REQ_CAMERA)) {
chooseCamera();
}
else {
Toast.makeText(getActivity(), getString(R.string.cancel_permission_read_contacts), Toast.LENGTH_SHORT).show();
}
}
public void setFileImageCatalog(boolean isCamera, File mFile, String imageContent) {
mFileImageCatalog = mFile;
Picasso mPic;
if(MyApiClient.PROD_FLAG_ADDRESS)
mPic = MyPicasso.getImageLoader(getActivity());
else
mPic= Picasso.with(getActivity());
if(mFileImageCatalog != null){
if(isCamera) {
mPic.load(new File(imageContent))
.error(R.drawable.icon_no_photo)
.placeholder(R.drawable.progress_animation)
.fit()
.centerInside()
.into(img_catalog);
}else
mPic.load(imageContent)
.error(R.drawable.icon_no_photo)
.fit()
.centerInside()
.placeholder(R.drawable.progress_animation)
.into(img_catalog);
}
}
StockBarangAction.java 是一个调用意图相机的片段,结果由 KantinActivity(Extend BaseActivity) 捕获,其中图像使用“GeneralizeImage”函数调整大小,然后使用“setFileImageCatalog”函数发送回片段。
我想知道的是:
- 三星 Galaxy S5 上的相机应用程序真的有问题吗?
- 为什么我的应用程序被杀死并且某些组件变为空(如 TextView)。我再次尝试“findViewId”,但它仍然无法正常工作。我需要保存 textview 实例吗?
谢谢你,对不起我的英语。