我正在使用此代码在Android应用程序中打开一个库。
Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, GALLERY);
它会打开图库但是当我从那里选择任何图像时它也会关闭,但不会被称为onActivityResult
方法。
方法就在这里......
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v(TAG, "requestCode = "+requestCode+" resultCode = "+resultCode);
Log.v(TAG, "inside onActivityResult");
if (requestCode == GALLERY || requestCode == CAMERA) {
if (resultCode == RESULT_OK)
{
Log.v(TAG, "inside onActivityResult OK ");
Log.v("log_tag", "onactivity result: " + requestCode);
if(requestCode == CAMERA)
{
Log.v(TAG, "inside onActivityResult Camera");
file = getTempFile(this.getParent());
try {
m_bmOCRBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
FileOutputStream out = new FileOutputStream(file);
int width = m_bmOCRBitmap.getWidth();
int height = m_bmOCRBitmap.getHeight();
if(width>height)
{
calwidth = (int)((width * 250)/height);
calheight = 250;
}
else
{
calheight = (int)((height * 200)/width);
calwidth = 200;
}
Log.v(TAG, "calwidth = "+calwidth+" calheight = "+calheight);
bitmap = Bitmap.createScaledBitmap(m_bmOCRBitmap, calwidth, calheight, true);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
productImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(requestCode == GALLERY)
{
Log.v(TAG, "inside onActivityResult GALARY ");
String selectedImagePath = getPath(data.getData());
file = new File(selectedImagePath);
m_bmOCRBitmap = BitmapFactory.decodeFile(selectedImagePath);
Log.v("log_tag", "selectedImagePath:" + selectedImagePath + ":" );
Log.v("log", "first image : "+m_bmOCRBitmap);
try {
FileOutputStream out = new FileOutputStream(file);
int width = m_bmOCRBitmap.getWidth();
int height = m_bmOCRBitmap.getHeight();
if(width>height)
{
calwidth = (int)((width * 250)/height);
calheight = 250;
}
else
{
calheight = (int)((height * 200)/width);
calwidth = 200;
}
bitmap = Bitmap.createScaledBitmap(m_bmOCRBitmap, calwidth, calheight, true);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
productImage.setImageBitmap(bitmap);
} catch (Exception e) {
Log.v("log_tag", "Exception: " + e.toString());
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
调用Intent时的警告
01-04 12:21:06.434: D/PhoneWindow(367): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@4054fb60 has no id.
请注意,我已将此活动作为Tabgoup活动中的子活动开始。
请建议我。感谢。
答案 0 :(得分:5)
onActivityResult()
未触发的可能性很多。
android:launchMode="singleInstance"
的manifest.xml startActivityOnResult
代替活动答案 1 :(得分:3)
我在此类
中添加了此类方法@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ProductImageActivity activity = (ProductImageActivity)getLocalActivityManager().getCurrentActivity();
try {
activity.onActivityResult(requestCode, resultCode, data,0);
} catch (Exception e) {
e.printStackTrace();
}
}
在我目前的课程中,我开始了这样的新意图。
Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
getParent().startActivityForResult(intent, GALLERY);
答案 2 :(得分:2)
检查http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK以了解Intent.FLAG_ACTIVITY_NEW_TASK的行为。它用于独立活动。
This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.
删除该行后,一旦选择了图像,就会调用onActivityResult。
Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY);
答案 3 :(得分:0)
试试这个&使用Intent
像这样从Gallery
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALLERY);
或者
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY);