意图在 onActivityResult 中不起作用
Intent not working in onActivityResult
我正在尝试从我的第一个 Activity 的图库中获取图像,并希望在第二个 activity 的 ImageView 中得到结果图像。
这是第一个 Activity 的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageVew = (ImageView) findViewById(R.id.imageView);
}
public void useGalleryMethod(View view) {
//this is for picking Image from Gallery or file
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
onActivityResult() 在第一个 Class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("Bitmap",bitmap);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
第二个代码 Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
Intent getIntentInfo = getIntent();
if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = (Bitmap) getIntentInfo.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);
}else{
return;
}
}
该应用程序 运行 正确并且仅在第一个 Class 的 ImageView 中显示图库图像,并且不会使用 onActivityResult 中的 Intent Activity 第二个 Activity方法。
请让我知道我的代码有什么问题? ?
还是有其他方法,我没有朝着正确的方向前进?
用这个比你用的Parcelable更方便的方法:
方法一:你可以把它存到sd卡上取回。
或
方法二:
将位图转换为字节数组:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
通过 Intent 传递字节数组:-
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
从捆绑包中收到的 ByteArray 中获取位图:-
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
在第一个 activity 的 onActivityResult()
中替换这个 -
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
// Send Image URI istead
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("imageUri", data.getData().toString());
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
然后在第二 Activity 上做类似 -
的事情
if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(getIntentInfo.getStringExtra("imageUri")));
imageVew.setImageBitmap(bitmap);
}else{
return;
}
我正在尝试从我的第一个 Activity 的图库中获取图像,并希望在第二个 activity 的 ImageView 中得到结果图像。
这是第一个 Activity 的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageVew = (ImageView) findViewById(R.id.imageView);
}
public void useGalleryMethod(View view) {
//this is for picking Image from Gallery or file
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
onActivityResult() 在第一个 Class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("Bitmap",bitmap);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
第二个代码 Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
Intent getIntentInfo = getIntent();
if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = (Bitmap) getIntentInfo.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);
}else{
return;
}
}
该应用程序 运行 正确并且仅在第一个 Class 的 ImageView 中显示图库图像,并且不会使用 onActivityResult 中的 Intent Activity 第二个 Activity方法。
请让我知道我的代码有什么问题? ? 还是有其他方法,我没有朝着正确的方向前进?
用这个比你用的Parcelable更方便的方法:
方法一:你可以把它存到sd卡上取回。
或 方法二:
将位图转换为字节数组:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
通过 Intent 传递字节数组:-
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
从捆绑包中收到的 ByteArray 中获取位图:-
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
在第一个 activity 的 onActivityResult()
中替换这个 -
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
// Send Image URI istead
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("imageUri", data.getData().toString());
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
然后在第二 Activity 上做类似 -
的事情if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(getIntentInfo.getStringExtra("imageUri")));
imageVew.setImageBitmap(bitmap);
}else{
return;
}