3

PreferenceActivity に「画像を選択」があります。

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

startActivityForResult(i, IMAGE_SELECT); 

しかし、Gallery から画像を選択しても、 my には何も返されませ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>
4

3 に答える 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
            }
        }
    }
}
于 2011-06-26T13:46:23.550 に答える
1

交換

startActivityForResult(i, IMAGE_SELECT);  

getActivity().startActivityForResult(i, IMAGE_SELECT); 

そして出来上がり!

于 2015-06-04T09:56:53.160 に答える
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);
            }
        }
    }
于 2011-06-26T13:29:26.113 に答える