0

在我的一项活动中,您可以单击图像按钮,然后会打开一个对话框,要求您打开图库。打开后,您可以单击图库中的这些图像之一。

我想要将单击的图像设置为图像按钮的图像。即使用户关闭应用程序,该图像也应该保留在那里(当他再次打开应用程序时,所选图像应该仍然存在)。

我选择使用共享偏好来完成所有这些工作,但无法让它发挥作用。单击图库中的图像时出现错误。有什么建议么??

更新:我收到的错误是:不幸的是,进程 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 为空。在retrievePath 中,当您尝试将“img_path”从您的偏好中拉出时,默认值为null,所以我猜它在那里被设置为null。签入调试器以确保 mFileName 已根据您的首选项设置为有效的字符串。

于 2012-06-05T18:03:05.843 回答