我是Android新手,我正在写一个相册应用。我正在尝试从用户那里获取图像并将其存储在相册中。以下是我的AlbumOptions活动中的代码段:
public void add(View v){
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(i, "Choose Photo To Add"),RESULT_LOAD_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null)
{
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]);
final String picturePath = cursor.getString(columnIndex);
cursor.close();
//get a name for the photo
final EditText pName = new EditText(this);
new AlertDialog.Builder(this)
.setTitle("Create Album")
.setMessage("Enter album name:")
.setView(pName)
.setPositiveButton("Add Photo", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String name = pName.getText().toString();
//check if photo name already exists, if not create it
if(!album.checkName(name))
{
Photo p = new Photo(name, picturePath, "");
photoList.add(p);
adapter.notifyDataSetChanged();
}
//prompt error that photo name is taken
else
{
AlertDialog alertDialog = new AlertDialog.Builder(AlbumOptions.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Photo name already exists!");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
});
}
}
当用户单击添加按钮以将照片添加到打开的相册时,将调用add方法。但是,使用调试工具时,程序将通过startActivityForResult()
调用。我查看了各种帖子,看看出了什么问题,我无法弄清楚出了什么问题。 RESULT_LOAD_IMAGE
是> 0,我按照我见过的许多帖子的建议检查了我的清单文件。任何反馈都表示赞赏。
答案 0 :(得分:0)
尝试此代码。这可能会对您有所帮助。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==SELECT_IMAGE){
if(resultCode==RESULT_OK && null!=data){
//do whatever you want.
}
}
首先将@Override public更改为@Override protected。并设置了这个。
Intent image=new Intent();
image.setType("image/*");
image.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(image,"select file"),SELECT_IMAGE);
更改此行。您已指出RESULT_LOAD_IMAGE为> 0,将其更改为= 0;
public int SELECT_IMAGE=0;