4

I'm developing a phonegap application that uses the camera. In low memory situations, when the camera is launched, my application is killed by the system, sometimes without calling onDestroy() method (now I know that only onPause() is guaranteed).

I can override the onPause() method (in javascript or java) to store the app status, and recover it when the app is restarted. The problem is that the picture file uri is lost, and my application can't obtain it.

Do you know any way in Android for recalling to my callback function when the camera returns the picture uri, and my application has been killed? Any workaround?

I think that this problem is common to all android developments that uses startActivityForResult(), but I haven't found any solution.

Thanks in advance ;-)

4

2 に答える 2

0

startActivity の直前に、保存されたインテントにフラグを設定して、強制終了して再起動するのではなく、それを表示してみてください。

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

ここで私の投稿を参照してください: https://stackoverflow.com/a/29630548/2782404

于 2015-04-14T16:57:16.227 に答える
0

私の解決策は、ファイル uri を SharedPreferences に保存し、onResume で復元することです。

私は同じ状況に遭遇しました:写真リストがあり、写真リストの1つの写真フレームを押すと、Androidネイティブカメラアプリが呼び出されて写真が撮られます. 時々 (使用率の 2% のように) Android ネイティブ カメラ アプリから戻ると、写真が期待どおりにフォト フレームに表示されません。私は戸惑い、何が起こったのか理解できませんでした。私の同僚の 1 人が「開発者向けオプション」で「アクティビティを保持しない」を設定し、常にバグに遭遇するまで、それはアクティビティが強制終了される問題であることを知っていました。

以下は、私のソリューションを示すためのコードです。

public static class PhotoOnClickListener implements OnClickListener {
            ...
            intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            activity.fileUri[index] = getOutputMediaFileUri(MEDIA_TYPE_IMAGE, "xxxxxx");
            activity.saveKeyValue("game_photo_list_file_uri_" + index, activity.fileUri[index].toString());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, activity.fileUri[index]);
            activity.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            ...
    }

private void tryRecoverFromBeingKilledOfLowMemory() {
    String s;
    for (int i = 0; i < fileUri.length; i++) {
        s = readKey("game_photo_list_file_uri_" + i);
        if (s != null) {
            fileUri[i] = Uri.parse(s);
            updatePhoto(i);
        }
    }
}

@Override
protected void onResume() {
    super.onResume();

    if (readKey("from_game_main") != null) {
        removeKeysPrefixedBy("game_photo_list");
        removeKey("from_game_main");
        removeKey("uploader_id");
    }

    tryRecoverFromBeingKilledOfLowMemory();
}

コード内:

  1. readKey、saveKeyValue、removeKey、removeKeysPrefixedBy は、SharedPrefeneces への共通操作として機能する CommonActivity から継承されます。
  2. キー from_game_main は、現在のレジュームが正常であり、空の写真リストから開始する必要があることを示しています。キー from_game_main は、GameMainActivity の startActivity の直前に保存されます。それ以外の場合、現在のレジュームはメモリ不足による強制終了からの回復です。

それが役に立てば幸い。

于 2013-01-05T06:25:51.540 に答える