为什么startActivityForResult没有返回任何东西?

3

我在PreferenceActivity中有一个“选择图像”的选项:

Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, IMAGE_SELECT); 

但是当我从图库中选择一张图片时,它不会返回任何东西到我的PreferenceActivityonActivityResult中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Toast.makeText(getApplicationContext(), "THIS TOAST IS NOT SHOWING",
            Toast.LENGTH_LONG).show();

}

我需要做什么?在清单中添加一些内容吗?这是不可能的吗?

这是我的“整个”类(省略了不相关的代码):

package com.xuso.myapp;


import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;



public class Settings extends PreferenceActivity {

    protected static final int NEED_RESTART = 2;
    protected static final int IMAGE_SELECT = 3;
    private int bg_color; //There are a lot more

    private Context context = this;
    private int result = RESULT_CANCELED;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    Preference pref_bg_color; //There are a lot more



    public void getPrefs(){

        sp = getSharedPreferences("myapp_prefs", Activity.MODE_PRIVATE);
        editor = sp.edit();

        bg_color = sp.getInt("pref_bg_color", 0xff000000);
        //... and the rest of the GETs.

    }






    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);


        getPrefs();


        //A lot of declarations of listeners when Preferences pressed
        //...
        //...
            pref_bg_image.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                @Override
                public boolean onPreferenceClick(Preference preference) {

                    Intent i = new Intent(Intent.ACTION_PICK,
                               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, IMAGE_SELECT); 


                    return false;
                }
            });


        //...
    //...
    //More and more declarations of Preferences.





    }



    //This is for returning results with preferences changes to the Main activity
    private void getBackAndCommit()
    {
        Intent intent= getIntent();
        setResult(result, intent);
        finish();
        overridePendingTransition(0, 0);
    }


    //Commit changes on back pressed
    public void onBackPressed ()
    {

        getBackAndCommit(); 

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) { 
        case IMAGE_SELECT:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            }
        }

    }



}

这是我的清单:

<application android:icon="@drawable/myapp_icon" android:label="@string/app_name"
>
    <activity android:name=".Main"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>


    </activity>

    <activity android:name=".Settings"
              android:label="@string/app_name"
              >
    </activity>

</application>
3个回答

3
必须正常工作,添加整个类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class StackOverflowAppActivity extends Activity {
    private final int PICK_IMAGE = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, PICK_IMAGE);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {

        case PICK_IMAGE:
            if (resultCode == RESULT_OK) {
                // do your thing
            }
        }
    }
}

创建一个新的带有新活动的项目是可以的,但是在我的PreferenceActivity上就不行了。这就是为什么我认为我必须向整个PreferenceActivity类或清单添加一些内容。 - xuso
已添加到第一篇帖子中,几乎包含了我的整个Settings类和清单文件。继续思考manifest的<activity>是否缺少某些内容。 - xuso
尝试在您的onActivityResult方法中添加super.onActivityResult(requestCode, resultCode, data);,就像我的示例一样。 - evilone
我不知道为什么现在可以工作了 :O 。我发誓,之前我已经尝试过但是没有成功... 但是现在,它可以工作了... 非常感谢 @evilone ;) - xuso

1
替换

标签。

startActivityForResult(i, IMAGE_SELECT);  

带有。
getActivity().startActivityForResult(i, IMAGE_SELECT); 

而且,voila!!


0

使用以下代码,可能可以解决您的问题。

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case REQ_CODE_PICK_IMAGE:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            }
        }
    }

2
误报警。不起作用。 :( 顺便说一下,问题不在onActivityResult内的代码...因为这个方法从未被调用过。 - xuso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接