【问题标题】:onActivityResult not called under API level 17在 API 级别 17 下未调用 onActivityResult
【发布时间】:2015-02-03 10:15:18
【问题描述】:

我有一个用于将图像上传到服务器的类,但我无法从图库中选择图像。它在 API 级别 +17 上正常工作,但在 API 级别 -17 上无法正常工作

这些是我的代码

自定义Javascript类

public String uploadImage(){
    Activity activity = (Activity)context;
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");       
    activity.startActivityForResult(photoPickerIntent, 100);
...

主活动

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    if(requestCode == 100){
        try{
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();            
            Log.i("File", filePath);
            if (Build.VERSION.SDK_INT >= 17){
                HappyJavaScriptInterface.attachment = new File(filePath);      
                HappyJavaScriptInterface.uploadProcess = 1;
            }
            else
            {
                HappyJavaScriptInterfaceUnder17.attachment = new File(filePath);      
                HappyJavaScriptInterfaceUnder17.uploadProcess = 1;
            }
        }
        catch(Exception ex){
            if (Build.VERSION.SDK_INT >= 17){
                HappyJavaScriptInterface.uploadProcess = 1;
            }
            else
            {
                HappyJavaScriptInterfaceUnder17.uploadProcess = 1;
            }
        }
    }

它在 android 4.2.2 模拟器上正常工作,但在 2.3.3 上不工作

谢谢

【问题讨论】:

  • 问题是onActivityResult没有被调用还是什么?
  • 是的,API 级别 -17 不调用 onActivityResult
  • 有没有可能你也实现了 startActivityForResult() 的两种方式?
  • 他们有不同的类
  • 它们是相同的方法但不同的类

标签: java android android-intent android-4.2-jelly-bean android-2.3-gingerbread


【解决方案1】:

尝试在你的清单中为MainActivity设置android:noHistory="false"

【讨论】:

  • 这取决于他是否设置了它。通常,如果清单中没有指定 noHistory,则默认值为 false。
  • @Opiatefuchs 你的意思是假的,我知道但有时它碰巧有一个奇怪的行为,例如有时我无法在 android studio 上导出我的 apk,因为标志 debuggable 不是设置为 false,但它甚至没有设置为 true :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
相关资源
最近更新 更多