用相机拍照三星galaxy s5破坏活动

时间:2016-12-19 19:58:56

标签: java android android-fragments samsung-galaxy

我正在开发使用ImageView显示照片的应用。但拍摄照片后,它不会显示在ImageView中。此外,应用程序拍摄后应用程序关闭。我试用了我的Nexus 5(Marshmallow),三星Galaxy S4(Lollipop& CustomRom),小米Mi4,华硕Zenfone 1和联想平板电脑,它们都运行顺畅。以下是我尝试使用三星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是一个调用intent camera的片段,结果由KantinActivity(Extend BaseActivity)捕获,其中图像使用“GeneralizeImage”函数调整大小,然后使用“setFileImageCatalog”函数发送回片段。

我想知道的是:

  • Samsung Galaxy S5上的相机应用确实有问题吗?
  • 为什么我的应用程序被杀死,一些组件变为空(如TextView)。我再次尝试“findViewId”,但它仍然无法正常工作。我需要吗? 保存textview实例?

谢谢你,对不起我的英语。

1 个答案:

答案 0 :(得分:0)

最有可能的是,当您的应用处于后台时,您的流程正在终止。这是完全正常的。您将在需要时获得一个全新的过程,并且用户可能会返回之前曾进行过的任何活动。

你的应用程序需要处理这种情况,因为它不仅会在你拍照时发生 - 它也可能由于其他原因而发生:

  • 用户在您的应用中
  • 用户按HOME
  • 五分钟后,Android终止您的进程以释放系统RAM以用于其他进程
  • 在此之后两分钟,用户返回到您的应用程序(主屏幕启动器图标,概览/最近任务屏幕等)。
  相机应用程序三星galaxy s5确实有问题吗?

没有

  

我是否需要保存textview实例?

这是不可能的。