0

私のアクティビティの 1 つで、画像ボタンをクリックすると、ダイアログが開き、ギャラリーを開くように求められます。開いたら、ギャラリーでそれらの画像のいずれかをクリックできます。

クリックした画像を画像ボタンの画像として設定したい。この画像は、ユーザーがアプリを終了してもそこにとどまる必要があります (ユーザーがアプリを再度開いたときに、選択した画像がまだそこにあるはずです)。

このすべての作業を共有設定で行うことにしましたが、うまくいきません。ギャラリーの画像をクリックするとエラーが発生します。助言がありますか??

更新: 私が受け取ったエラーは次のとおりです: 残念ながら、プロセス com.myname.android が停止しました

onActivityResult で始まる私のコードは次のとおりです。

private String selectedImagePath;
private String mFileName;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_FROM_FILE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

            Log.v("IMAGE PATH====>>>> ",selectedImagePath);
        }
        storePath();
        retrievePath();
        convertPathToImage();
    }
}

private void storePath() {
    final SharedPreferences sPreference = getSharedPreferences(
            "pref_key", MODE_PRIVATE);
    final Editor spEditor = sPreference.edit();
    spEditor.putString("img_path", mFileName);
    spEditor.commit();
}

private void retrievePath() {
    final SharedPreferences sharedPreference = getSharedPreferences(
            "pref_key", MODE_PRIVATE);
    if (sharedPreference.contains("img_path")) {
        mFileName = sharedPreference.getString("img_path",
                null);
    }
}

private void convertPathToImage() {
    File imgFile = new  File(mFileName);
    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ImageButton myImage = (ImageButton) findViewById(R.id.image);
        myImage.setImageBitmap(myBitmap);

    }
}

LogCat は次のとおりです。

05-30 15:27:14.567: D/dalvikvm(634): GC_CONCURRENT freed 1K, 3% free 11478K/11783K, paused 5ms+4ms
05-30 15:27:14.597: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:14.617: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:14.926: D/gralloc_goldfish(634): Emulator without GPU emulation detected.
05-30 15:27:26.767: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:26.877: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:30.616: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:30.626: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:32.086: W/IInputConnectionWrapper(634): showStatusIcon on inactive InputConnection
05-30 15:27:33.366: E/ActivityThread(634): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@4155da80 that was originally registered here. Are you missing a call to unregisterReceiver()?
05-30 15:27:33.366: E/ActivityThread(634): android.app.IntentReceiverLeaked: Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@4155da80 that was originally registered here. Are you missing a call to unregisterReceiver()?
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1043)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:1030)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:1024)
05-30 15:27:33.366: E/ActivityThread(634):  at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:341)
05-30 15:27:33.366: E/ActivityThread(634):  at com.android.internal.content.PackageMonitor.register(PackageMonitor.java:65)
05-30 15:27:33.366: E/ActivityThread(634):  at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:99)
05-30 15:27:33.366: E/ActivityThread(634):  at com.android.internal.app.ChooserActivity.onCreate(ChooserActivity.java:53)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.Activity.performCreate(Activity.java:4465)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-30 15:27:33.366: E/ActivityThread(634):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 15:27:33.366: E/ActivityThread(634):  at android.os.Looper.loop(Looper.java:137)
05-30 15:27:33.366: E/ActivityThread(634):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-30 15:27:33.366: E/ActivityThread(634):  at java.lang.reflect.Method.invokeNative(Native Method)
05-30 15:27:33.366: E/ActivityThread(634):  at java.lang.reflect.Method.invoke(Method.java:511)
05-30 15:27:33.366: E/ActivityThread(634):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 15:27:33.366: E/ActivityThread(634):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 15:27:33.366: E/ActivityThread(634):  at dalvik.system.NativeStart.main(Native Method)
05-30 15:27:35.987: V/IMAGE PATH====>>>>(634): /mnt/sdcard/Mercedes_SLS_AMG.jpg
05-30 15:27:36.176: D/AndroidRuntime(634): Shutting down VM
05-30 15:27:36.176: W/dalvikvm(634): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-30 15:27:36.206: E/AndroidRuntime(634): FATAL EXCEPTION: main
05-30 15:27:36.206: E/AndroidRuntime(634): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/20 }} to activity {com.xyz.android.taskreminder/com.xyz.android.taskreminder.ReminderEditActivity}: java.lang.NullPointerException
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.app.ActivityThread.access$1100(ActivityThread.java:123)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.os.Looper.loop(Looper.java:137)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-30 15:27:36.206: E/AndroidRuntime(634):  at java.lang.reflect.Method.invokeNative(Native Method)
05-30 15:27:36.206: E/AndroidRuntime(634):  at java.lang.reflect.Method.invoke(Method.java:511)
05-30 15:27:36.206: E/AndroidRuntime(634):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 15:27:36.206: E/AndroidRuntime(634):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 15:27:36.206: E/AndroidRuntime(634):  at dalvik.system.NativeStart.main(Native Method)
05-30 15:27:36.206: E/AndroidRuntime(634): Caused by: java.lang.NullPointerException
05-30 15:27:36.206: E/AndroidRuntime(634):  at java.io.File.fixSlashes(File.java:185)
05-30 15:27:36.206: E/AndroidRuntime(634):  at java.io.File.<init>(File.java:134)
05-30 15:27:36.206: E/AndroidRuntime(634):  at com.xyz.android.taskreminder.ReminderEditActivity.convertPathToImage(ReminderEditActivity.java:249)
05-30 15:27:36.206: E/AndroidRuntime(634):  at com.xyz.android.taskreminder.ReminderEditActivity.onActivityResult(ReminderEditActivity.java:227)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.app.Activity.dispatchActivityResult(Activity.java:4649)
05-30 15:27:36.206: E/AndroidRuntime(634):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
05-30 15:27:36.206: E/AndroidRuntime(634):  ... 11 more
05-30 15:27:36.376: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:36.396: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:36.787: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:36.847: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:37.386: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:37.406: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:37.887: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:37.947: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:38.396: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:38.416: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:38.897: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:38.927: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:39.406: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:39.426: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:39.917: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:39.927: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:40.426: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:40.446: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:40.917: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:40.947: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:41.426: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:41.447: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:41.937: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:41.977: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:42.446: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:42.467: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:42.937: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:42.956: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:43.446: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:43.467: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:43.947: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:43.976: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:44.456: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:44.477: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:44.966: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:44.986: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
05-30 15:27:45.466: I/dalvikvm(634): threadid=3: reacting to signal 3
05-30 15:27:45.487: I/dalvikvm(634): Wrote stack traces to '/data/anr/traces.txt'
4

1 に答える 1

1
Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@4155da80 that was originally registered here. Are you missing a call to unregisterReceiver()?

これはおそらく Android のバグです。http://code.google.com/p/android/issues/detail?id=29399を参照してください。アプリには影響しませんが、ギャラリーから結果を取得できるはずです。

05-30 15:27:36.206: E/AndroidRuntime(634): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/20 }} to activity {com.ndroidstudios.android.taskreminder/com.ndroidstudios.android.taskreminder.ReminderEditActivity}: java.lang.NullPointerException
...
05-30 15:27:36.206: E/AndroidRuntime(634): Caused by: java.lang.NullPointerException

これが問題です。mFileName を使用して新しいファイルを作成しようとすると、null になります。retrievePath で、設定から「img_path」を取り出そうとすると、デフォルト値が null なので、そこで null に設定されていると思います。デバッガーをチェックインして、設定から mFileName が有効な文字列に設定されていることを確認します。

于 2012-06-05T18:03:05.843 に答える