我正在尝试创建一个打开Gallery的意图(工作正常)。
但是,我需要更改图像按钮以显示用户选择的图像。
问题是在galleryIntent()方法之前调用了onActivityResult()方法,导致结果代码不是RESULT_OK,因此当选择图片时,用户被重定向到应用程序并且没有任何反应。
public class DataEntryActivity extends AppCompatActivity {
ImageButton imgButton;
Bitmap bmp;
private static final int PICTURE_SELECTED = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.bernine.practicalsessions.R.layout.activity_portal);
imgButton = (ImageButton) findViewById(com.example.bernine.practicalsessions.R.id.imageButton1);
}
//Method to start the intent upon clicking the imageButton
public void galleryIntent(View view) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, PICTURE_SELECTED);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getApplicationContext(), "Image Opened Before", Toast.LENGTH_LONG).show();
if (resultCode == RESULT_OK) {
try {
InputStream stream = getContentResolver().openInputStream(data.getData());
bmp = BitmapFactory.decodeStream(stream);
stream.close();
Toast.makeText(getApplicationContext(), "Image Opened", Toast.LENGTH_LONG).show();
imgButton.setImageBitmap(bmp);
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
}
}else
{
Toast.makeText(getApplicationContext(), "Result not ok", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
我尝试做android:launchMode ="标准"在清单中的活动但没有奏效。
答案 0 :(得分:0)
您不必为图片选择开始新任务 -
public void galleryIntent(View view) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
**//Remove this line**
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, PICTURE_SELECTED);
}