以下是代码:
package com.survey.sop;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class signup extends Activity
{
private static int RESULT_LOAD_IMAGE = 1;
SQLiteDatabase myDB = null;
TextView name ;
TextView address;
TextView birthdate;
TextView email;
TextView username;
TextView password;
Button signup;
ImageView imgAvatar;
int id_To_Update = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
name = (TextView) findViewById(R.id.txtname);
address = (TextView) findViewById(R.id.txtaddress);
birthdate = (TextView) findViewById(R.id.txtbdate);
email = (TextView) findViewById(R.id.txtemail);
username = (TextView) findViewById(R.id.txtuser);
password = (TextView) findViewById(R.id.txtpwd);
signup = (Button) findViewById(R.id.btnRegister);
imgAvatar = (ImageView) findViewById(R.id.imgAvatar);
myDB = this.openOrCreateDatabase("surveyDB", MODE_PRIVATE, null);
/////////////////////////////////////////////////////////////////////////
imgAvatar.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
///////////////////////////////////////////////////////////////////////////
signup.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
String usernameStr = username.getText().toString();
String passwordStr = password.getText().toString();
String nameStr = name.getText().toString();
String addressStr = address.getText().toString();
String birthdateStr = birthdate.getText().toString();
if (usernameStr.equals("") || passwordStr.equals("") || nameStr.equals("") || address.equals("") || birthdate.equals(""))
{
Toast.makeText(getBaseContext(), "Please fill all the required fields.", 2).show();
}
else
{
myDB.execSQL("INSERT INTO Users " + "(username, userpwd, name, address, birthdate, level)"
+ " VALUES ('" + usernameStr + "', '" + passwordStr + "','"
+ nameStr + "', '" + addressStr + "','"
+ birthdateStr + "', 'user');");
Toast.makeText(getBaseContext(), "Successfully registered!", 2).show();
username.setText("");
password.setText("");
name.setText("");
address.setText("");
birthdate.setText("");
}
}
catch (Exception e)
{
Log.e("Error", "Error", e);
Toast.makeText(getBaseContext(), "Error signing up.", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri selectedImageUri = data.getData();
String s = getRealPathFromURI(selectedImageUri);
//testing
Toast.makeText(getBaseContext(), s, 4).show();
}
}
private String getRealPathFromURI(Uri uri)
{
// TODO Auto-generated method stub
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
当我点击图片时,会打开图库,我可以选择图片。
我想测试我的代码是否可以获取所选图像目录的字符串,以便我可以存储/调用它
来自数据库 - 遗憾的是,在我选择图片后,该应用程序将关闭。
我对Android编程知之甚少,还是一个新编程。
提前致谢。
这是logcat:
06-26 15:18:56.218: V/PhoneWindow(26324): DecorView setVisiblity: visibility = 4
06-26 15:18:56.227: V/PhoneWindow(26324): DecorView setVisiblity: visibility = 0
06-26 15:18:56.256: V/InputMethodManager(26324): onWindowFocus: null softInputMode=288 first=true flags=#8010500
06-26 15:18:56.256: V/InputMethodManager(26324): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{42729b50 V.E..... R.....ID 0,0-480,800} ic=null tba=android.view.inputmethod.EditorInfo@4272e388 controlFlags=#104
06-26 15:18:56.259: V/InputMethodManager(26324): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@4272ec18 com.android.inputmethod.latin/.LatinIME #1252}
06-26 15:18:56.295: I/InputMethodManager(26324): handleMessage: MSG_SET_ACTIVE true, was false
06-26 15:18:59.298: V/PhoneWindow(26324): DecorView setVisiblity: visibility = 4
06-26 15:18:59.301: V/PhoneWindow(26324): DecorView setVisiblity: visibility = 0
06-26 15:18:59.307: V/InputMethodManager(26324): Not IME target window, ignoring
06-26 15:18:59.327: V/InputMethodManager(26324): onWindowFocus: android.widget.EditText{42736968 VFED..CL .F....ID 24,368-456,440 #7f080007 app:id/txtlUsername} softInputMode=288 first=true flags=#8010500
06-26 15:18:59.328: V/InputMethodManager(26324): START INPUT: android.widget.EditText{42736968 VFED..CL .F....ID 24,368-456,440 #7f080007 app:id/txtlUsername} ic=com.android.internal.widget.EditableInputConnection@4273ed18 tba=android.view.inputmethod.EditorInfo@4273ec08 controlFlags=#107
06-26 15:18:59.331: V/InputMethodManager(26324): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@4273f8f8 com.android.inputmethod.latin/.LatinIME #1253}
06-26 15:19:00.847: I/SurfaceTextureClient(26324): [STC::queueBuffer] (this:0x522866f0) fps:2.02, dur:1488.66, max:512.32, min:483.63
06-26 15:19:01.653: V/Provider/Settings(26324): from settings cache , name = sound_effects_enabled , value = 0
06-26 15:19:01.654: V/InputMethodManager(26324): Not IME target window, ignoring
06-26 15:19:01.673: V/InputMethodManager(26324): Not IME target window, ignoring
06-26 15:19:01.704: V/InputMethodManager(26324): focusOut: android.widget.EditText{42736968 VFED..CL .F....I. 24,368-456,440 #7f080007 app:id/txtlUsername} mServedView=android.widget.EditText{42736968 VFED..CL .F....I. 24,368-456,440 #7f080007 app:id/txtlUsername} winFocus=false
06-26 15:19:02.352: I/SurfaceTextureClient(26324): [STC::queueBuffer] (this:0x522866f0) fps:1.99, dur:1505.53, max:617.73, min:387.83
06-26 15:19:02.357: V/InputMethodManager(26324): onWindowFocus: android.widget.EditText{42736968 VFED..CL .F....ID 24,368-456,440 #7f080007 app:id/txtlUsername} softInputMode=32 first=false flags=#8010500
06-26 15:19:02.360: V/InputMethodManager(26324): focusOut: android.widget.EditText{42736968 VFED..CL .F....ID 24,368-456,440 #7f080007 app:id/txtlUsername} mServedView=android.widget.EditText{42736968 VFED..CL .F....ID 24,368-456,440 #7f080007 app:id/txtlUsername} winFocus=false
06-26 15:19:02.391: V/PhoneWindow(26324): DecorView setVisiblity: visibility = 4
06-26 15:19:02.395: V/PhoneWindow(26324): DecorView setVisiblity: visibility = 0
06-26 15:19:02.448: V/InputMethodManager(26324): onWindowFocus: android.widget.EditText{42750b90 VFED..CL .F....ID 0,203-480,275 #7f08000a app:id/txtname} softInputMode=288 first=true flags=#8010500
06-26 15:19:02.449: D/InputMethodManager(26324): deactivate the inputconnection in ControlledInputConnectionWrapper.
06-26 15:19:02.450: V/InputMethodManager(26324): START INPUT: android.widget.EditText{42750b90 VFED..CL .F....ID 0,203-480,275 #7f08000a app:id/txtname} ic=com.android.internal.widget.EditableInputConnection@42762a48 tba=android.view.inputmethod.EditorInfo@42762938 controlFlags=#107
06-26 15:19:02.452: V/InputMethodManager(26324): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@427635f0 com.android.inputmethod.latin/.LatinIME #1254}
06-26 15:19:03.417: V/Provider/Settings(26324): from settings cache , name = sound_effects_enabled , value = 0
06-26 15:19:03.493: V/InputMethodManager(26324): focusOut: android.widget.EditText{42750b90 VFED..CL .F....I. 0,203-480,275 #7f08000a app:id/txtname} mServedView=android.widget.EditText{42750b90 VFED..CL .F....I. 0,203-480,275 #7f08000a app:id/txtname} winFocus=false
06-26 15:19:03.518: I/SurfaceTextureClient(26324): [STC::queueBuffer] (this:0x52524000) fps:2.92, dur:1025.88, max:529.50, min:25.97
06-26 15:19:03.701: I/InputMethodManager(26324): handleMessage: MSG_SET_ACTIVE false, was true
06-26 15:19:03.702: I/InputMethodManager(26324): handleMessage: MSG_UNBIND 1254
06-26 15:19:03.703: D/InputMethodManager(26324): deactivate the inputconnection in ControlledInputConnectionWrapper.
06-26 15:19:03.703: W/IInputConnectionWrapper(26324): showStatusIcon on inactive InputConnection
答案 0 :(得分:0)
如果调用onActivityResult()
,请检查requestCode是否有效。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == RESULT_LOAD_IMAGE)
{
Uri selectedImageUri = data.getData();
String s = getRealPathFromURI(selectedImageUri);
//testing
Toast.makeText(getBaseContext(), s, 4).show();
}
}
如果你的设备内存不足,你必须保存一些在调用startActivityForResult
之前使用的成员变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
}
else {
// just sample member variables
mOutputFileUri = Uri.parse(savedInstanceState.getString("mOutputFileUri", ""));
mStep = savedInstanceState.getInt("mStep");
mCropSizeX = savedInstanceState.getInt("mCropSizeX");
mCropSizeY = savedInstanceState.getInt("mCropSizeY");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("mOutputFileUri", (mOutputFileUri != null)? mOutputFileUri.toString(): "");
outState.putInt("mStep", mStep);
outState.putInt("mCropSizeX", mCropSizeX);
outState.putInt("mCropSizeY", mCropSizeY);
}