2017-02-17 129 views
0

我製作了一個訪問相機的android AARplugins。 但它不會激活onActivityResult[Unity 3D]我想要訪問Android相機並拿起路徑

public static void showCamera ()  // Connect Camera 
{ 
    String Uri; 

    Activity CameraActivity = UnityPlayer.currentActivity; 

    Toast.makeText(CameraActivity , "first" ,Toast.LENGTH_LONG).show(); 
    Log.d(" before [email protected]@@@@@ ", "first"); 


CameraActivity.runOnUiThread(new Runnable() 
{ 
    public void run() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     CameraActivity.startActivityForResult(intent,1); 
    } 
}); 
} 

///////////////不要↓激活/////////////////////// ///////////

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data)       
{ 
    Log.d(" before [email protected]@@@@@ ", "twotwotwotwotwotwotwo"); //Don't activate 
} 

//////////////////////////////// ////////////////////////////////////////////

why why不激活這個?

+0

接入攝像頭成功。但'dose't主動'onActivityResult()'功能 – user7578245

回答

0

試試這個,

相機許可

private static final int REQUEST_CAMERA = 112; 


    btn_cam.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (Build.VERSION.SDK_INT >= 23) { 
         String[] PERMISSIONS = {android.Manifest.permission.CAMERA}; 
         if (!hasPermissions(mContext, PERMISSIONS)) { 
          ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_CAMERAE); 
         } else { 

          //open camera 
         } 
        } else { 

         //open camera 
        } 

       } 
      }); 
     } 

獲取權限結果

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case REQUEST_CAMERA: { 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        //open camera 
       } 
      } 
     } 
    } 

的棉花糖檢查權限

private static boolean hasPermissions(Context context, String... permissions) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) { 
      for (String permission : permissions) { 
       if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { 
        return false; 
       } 
      } 
     } 
     return true; 
    } 

AndroidManifest.xml中

<uses-permission android:name="android.permission.CAMERA" /> 
+0

我訪問攝像機成功。但'dose'不活躍'onActivityResult()'功能。 – user7578245

+0

在活動或片段中調用startActivityForResult的位置? – user2025187