I am working on app which uses an ImageView to display a photo. But after a photo is taken, it is not being shown in the ImageView. Furthermore, the app gets force closed after taking the picture. I try this with my Nexus 5 (Marshmallow), Samsung Galaxy S4(Lollipop & CustomRom), Xiaomi Mi4, Asus Zenfone 1, and Lenovo tablet which all run smoothly. Here are some scenarios that I tried with the Samsung Galaxy S5 :
- Take a photo, wait a couple of seconds before clicking "Ok", then I press back to my apps. (Result : sometimes this works, sometimes image does not show in the ImageView)
- Take a photo and immediately click "Ok". (Result : force closed. Logcat says TextView in abstract class is null )
- Take a photo, immediately click "Ok" while debuging in function OnActivityResult (Result : it works)
Here's the code :
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 is a fragment that call intent camera, the result catch by KantinActivity(Extend BaseActivity) where the image get resize using "GeneralizeImage" function, then sent back to fragment with "setFileImageCatalog" function.
What I want to know is :
- does the camera app on the Samsung Galaxy S5 really have problem ?
- Why my apps get killed and some components become null (like TextView). I try to "findViewId" again, but its still not working. Do i need to save the textview instance?
Thank you and sorry for my English.
Most likely, your process is being terminated while your app is in the background. This is perfectly normal. You will get a fresh process when needed, and the user may be returned to whatever activity they had been on previously.
Your app needs to handle this case, because it is not only going to happen when you take a picture — it can happen for other reasons as well:
No.
That is not possible.