startActivityForResult不返回任何东西(从preferenceActivity影像撷取) [英] startActivityForResult does not returns anything (Image Picking from a PreferenceActivity)
本文介绍了startActivityForResult不返回任何东西(从preferenceActivity影像撷取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个preferenceActivity一个选择图片:
意向书我=新的意图(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(ⅰ,IMAGE_SELECT);
但是,当我选择从画廊的图像,它不会给我的preferenceActivity的onActivityResult返回任何内容:
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
Toast.makeText(getApplicationContext()这个土司没有显示,
Toast.LENGTH_LONG).show();
}
还有什么我需要做什么?添加东西的清单?它是不可能的?
我希望你能帮助我,谢谢)
编辑:
这是我的整体类(省略了不相关的code):
包com.xuso.myapp;
进口yuku.ambilwarna.AmbilWarnaDialog;
进口yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.app.AlertDialog.Builder;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.Bundle;
。进口的Android preference.CheckBox preference;
进口的Android preference.List preference。
。进口的Android preference preference;
。进口的Android preference preference.On preferenceChangeListener。
。进口的Android preference preference.On preferenceClickListener。
。进口的Android preference preferenceActivity;
进口android.provider.MediaStore;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.widget.SeekBar;
进口android.widget.SeekBar.OnSeekBarChangeListener;
进口android.widget.TextView;
进口android.widget.Toast;
公共类设置来延长preferenceActivity {
受保护的静态最终诠释NEED_RESTART = 2;
受保护的静态最终诠释IMAGE_SELECT = 3;
私人诠释bg_color; //还有很多更
私人上下文的背景下=这一点;
私人诠释结果= RESULT_CANCELED;
私人共享preferences SP;
私人共享preferences.Editor编辑;
preference pref_bg_color; //还有很多更
公共无效的get preFS(){
SP = getShared preferences(myapp_ preFS,Activity.MODE_PRIVATE);
编辑= sp.edit();
bg_color = sp.getInt(pref_bg_color,0xff000000);
// ...和的其余愈大。
}
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml.settings);
获得preFS();
//很多听众的声明时,preferences pressed
// ...
// ...
pref_bg_image.setOn preferenceClickListener(新在preferenceClickListener(){
@覆盖
公共布尔在preferenceClick(preference preference){
意图I =新的意图(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(ⅰ,IMAGE_SELECT);
返回false;
}
});
// ...
// ...
//越来越多的声明preferences。
}
//这是返回与preferences改变结果的主要活动
私人无效getBackAndCommit()
{
意向意图= getIntent();
的setResult(结果,意图);
完();
overridePendingTransition(0,0);
}
//提交背面pressed变化
公共无效onBack pressed()
{
getBackAndCommit();
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
开关(要求code){
案例IMAGE_SELECT:
如果(结果code == RESULT_OK){
乌里selectedImage = data.getData();
的String [] filePathColumn = {MediaStore.Images.Media.DATA};
光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst();
INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
字符串文件路径= cursor.getString(参数:columnIndex);
cursor.close();
位图yourSelectedImage = BitmapFactory.de codeFILE(文件路径);
}
}
}
}
这是我的清单:
<应用机器人:图标=@可绘制/ myapp_icon机器人:标签=@字符串/ APP_NAME
>
<活动机器人:主名称=
机器人:标签=@字符串/ APP_NAME
机器人:主题=@安卓风格/ Theme.NoTitleBar
>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:设置名称=
机器人:标签=@字符串/ APP_NAME
>
< /活性GT;
< /用途>
解决方案
必须工作,加入全班同学:
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
公共类StackOverflowAppActivity延伸活动{
私人最终诠释PICK_IMAGE = 0;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
意图I =新的意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(ⅰ,PICK_IMAGE);
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
开关(要求code){
案例PICK_IMAGE:
如果(结果code == RESULT_OK){
//做你的事
}
}
}
}
I have a "Select image" in a PreferenceActivity:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, IMAGE_SELECT);
But when I pick an image from Gallery, it does not return anything to my PreferenceActivity's onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getApplicationContext(), "THIS TOAST IS NOT SHOWING",
Toast.LENGTH_LONG).show();
}
What more I need to do? Add something to the manifest? Is it impossible?
I hope you can help me, thanks ;)
EDIT:
This is my "whole" class (omited irrelevant code):
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);
}
}
}
}
And this is my manifest:
<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>
解决方案
Must work, added whole class:
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
}
}
}
}
这篇关于startActivityForResult不返回任何东西(从preferenceActivity影像撷取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文